nvram_64.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254
  1. /*
  2. * c 2001 PPC 64 Team, IBM Corp
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. *
  9. * /dev/nvram driver for PPC64
  10. *
  11. * This perhaps should live in drivers/char
  12. *
  13. * TODO: Split the /dev/nvram part (that one can use
  14. * drivers/char/generic_nvram.c) from the arch & partition
  15. * parsing code.
  16. */
  17. #include <linux/module.h>
  18. #include <linux/types.h>
  19. #include <linux/errno.h>
  20. #include <linux/fs.h>
  21. #include <linux/miscdevice.h>
  22. #include <linux/fcntl.h>
  23. #include <linux/nvram.h>
  24. #include <linux/init.h>
  25. #include <linux/slab.h>
  26. #include <linux/spinlock.h>
  27. #include <linux/kmsg_dump.h>
  28. #include <linux/pstore.h>
  29. #include <linux/zlib.h>
  30. #include <asm/uaccess.h>
  31. #include <asm/nvram.h>
  32. #include <asm/rtas.h>
  33. #include <asm/prom.h>
  34. #include <asm/machdep.h>
  35. #undef DEBUG_NVRAM
  36. #define NVRAM_HEADER_LEN sizeof(struct nvram_header)
  37. #define NVRAM_BLOCK_LEN NVRAM_HEADER_LEN
  38. /* If change this size, then change the size of NVNAME_LEN */
  39. struct nvram_header {
  40. unsigned char signature;
  41. unsigned char checksum;
  42. unsigned short length;
  43. /* Terminating null required only for names < 12 chars. */
  44. char name[12];
  45. };
  46. struct nvram_partition {
  47. struct list_head partition;
  48. struct nvram_header header;
  49. unsigned int index;
  50. };
  51. static LIST_HEAD(nvram_partitions);
  52. #ifdef CONFIG_PPC_PSERIES
  53. struct nvram_os_partition rtas_log_partition = {
  54. .name = "ibm,rtas-log",
  55. .req_size = 2079,
  56. .min_size = 1055,
  57. .index = -1,
  58. .os_partition = true
  59. };
  60. #endif
  61. struct nvram_os_partition oops_log_partition = {
  62. .name = "lnx,oops-log",
  63. .req_size = 4000,
  64. .min_size = 2000,
  65. .index = -1,
  66. .os_partition = true
  67. };
  68. static const char *nvram_os_partitions[] = {
  69. #ifdef CONFIG_PPC_PSERIES
  70. "ibm,rtas-log",
  71. #endif
  72. "lnx,oops-log",
  73. NULL
  74. };
  75. static void oops_to_nvram(struct kmsg_dumper *dumper,
  76. enum kmsg_dump_reason reason);
  77. static struct kmsg_dumper nvram_kmsg_dumper = {
  78. .dump = oops_to_nvram
  79. };
  80. /*
  81. * For capturing and compressing an oops or panic report...
  82. * big_oops_buf[] holds the uncompressed text we're capturing.
  83. *
  84. * oops_buf[] holds the compressed text, preceded by a oops header.
  85. * oops header has u16 holding the version of oops header (to differentiate
  86. * between old and new format header) followed by u16 holding the length of
  87. * the compressed* text (*Or uncompressed, if compression fails.) and u64
  88. * holding the timestamp. oops_buf[] gets written to NVRAM.
  89. *
  90. * oops_log_info points to the header. oops_data points to the compressed text.
  91. *
  92. * +- oops_buf
  93. * | +- oops_data
  94. * v v
  95. * +-----------+-----------+-----------+------------------------+
  96. * | version | length | timestamp | text |
  97. * | (2 bytes) | (2 bytes) | (8 bytes) | (oops_data_sz bytes) |
  98. * +-----------+-----------+-----------+------------------------+
  99. * ^
  100. * +- oops_log_info
  101. *
  102. * We preallocate these buffers during init to avoid kmalloc during oops/panic.
  103. */
  104. static size_t big_oops_buf_sz;
  105. static char *big_oops_buf, *oops_buf;
  106. static char *oops_data;
  107. static size_t oops_data_sz;
  108. /* Compression parameters */
  109. #define COMPR_LEVEL 6
  110. #define WINDOW_BITS 12
  111. #define MEM_LEVEL 4
  112. static struct z_stream_s stream;
  113. #ifdef CONFIG_PSTORE
  114. #ifdef CONFIG_PPC_POWERNV
  115. static struct nvram_os_partition skiboot_partition = {
  116. .name = "ibm,skiboot",
  117. .index = -1,
  118. .os_partition = false
  119. };
  120. #endif
  121. #ifdef CONFIG_PPC_PSERIES
  122. static struct nvram_os_partition of_config_partition = {
  123. .name = "of-config",
  124. .index = -1,
  125. .os_partition = false
  126. };
  127. #endif
  128. static struct nvram_os_partition common_partition = {
  129. .name = "common",
  130. .index = -1,
  131. .os_partition = false
  132. };
  133. static enum pstore_type_id nvram_type_ids[] = {
  134. PSTORE_TYPE_DMESG,
  135. PSTORE_TYPE_PPC_COMMON,
  136. -1,
  137. -1,
  138. -1
  139. };
  140. static int read_type;
  141. #endif
  142. /* nvram_write_os_partition
  143. *
  144. * We need to buffer the error logs into nvram to ensure that we have
  145. * the failure information to decode. If we have a severe error there
  146. * is no way to guarantee that the OS or the machine is in a state to
  147. * get back to user land and write the error to disk. For example if
  148. * the SCSI device driver causes a Machine Check by writing to a bad
  149. * IO address, there is no way of guaranteeing that the device driver
  150. * is in any state that is would also be able to write the error data
  151. * captured to disk, thus we buffer it in NVRAM for analysis on the
  152. * next boot.
  153. *
  154. * In NVRAM the partition containing the error log buffer will looks like:
  155. * Header (in bytes):
  156. * +-----------+----------+--------+------------+------------------+
  157. * | signature | checksum | length | name | data |
  158. * |0 |1 |2 3|4 15|16 length-1|
  159. * +-----------+----------+--------+------------+------------------+
  160. *
  161. * The 'data' section would look like (in bytes):
  162. * +--------------+------------+-----------------------------------+
  163. * | event_logged | sequence # | error log |
  164. * |0 3|4 7|8 error_log_size-1|
  165. * +--------------+------------+-----------------------------------+
  166. *
  167. * event_logged: 0 if event has not been logged to syslog, 1 if it has
  168. * sequence #: The unique sequence # for each event. (until it wraps)
  169. * error log: The error log from event_scan
  170. */
  171. int nvram_write_os_partition(struct nvram_os_partition *part,
  172. char *buff, int length,
  173. unsigned int err_type,
  174. unsigned int error_log_cnt)
  175. {
  176. int rc;
  177. loff_t tmp_index;
  178. struct err_log_info info;
  179. if (part->index == -1)
  180. return -ESPIPE;
  181. if (length > part->size)
  182. length = part->size;
  183. info.error_type = cpu_to_be32(err_type);
  184. info.seq_num = cpu_to_be32(error_log_cnt);
  185. tmp_index = part->index;
  186. rc = ppc_md.nvram_write((char *)&info, sizeof(struct err_log_info),
  187. &tmp_index);
  188. if (rc <= 0) {
  189. pr_err("%s: Failed nvram_write (%d)\n", __func__, rc);
  190. return rc;
  191. }
  192. rc = ppc_md.nvram_write(buff, length, &tmp_index);
  193. if (rc <= 0) {
  194. pr_err("%s: Failed nvram_write (%d)\n", __func__, rc);
  195. return rc;
  196. }
  197. return 0;
  198. }
  199. /* nvram_read_partition
  200. *
  201. * Reads nvram partition for at most 'length'
  202. */
  203. int nvram_read_partition(struct nvram_os_partition *part, char *buff,
  204. int length, unsigned int *err_type,
  205. unsigned int *error_log_cnt)
  206. {
  207. int rc;
  208. loff_t tmp_index;
  209. struct err_log_info info;
  210. if (part->index == -1)
  211. return -1;
  212. if (length > part->size)
  213. length = part->size;
  214. tmp_index = part->index;
  215. if (part->os_partition) {
  216. rc = ppc_md.nvram_read((char *)&info,
  217. sizeof(struct err_log_info),
  218. &tmp_index);
  219. if (rc <= 0) {
  220. pr_err("%s: Failed nvram_read (%d)\n", __func__, rc);
  221. return rc;
  222. }
  223. }
  224. rc = ppc_md.nvram_read(buff, length, &tmp_index);
  225. if (rc <= 0) {
  226. pr_err("%s: Failed nvram_read (%d)\n", __func__, rc);
  227. return rc;
  228. }
  229. if (part->os_partition) {
  230. *error_log_cnt = be32_to_cpu(info.seq_num);
  231. *err_type = be32_to_cpu(info.error_type);
  232. }
  233. return 0;
  234. }
  235. /* nvram_init_os_partition
  236. *
  237. * This sets up a partition with an "OS" signature.
  238. *
  239. * The general strategy is the following:
  240. * 1.) If a partition with the indicated name already exists...
  241. * - If it's large enough, use it.
  242. * - Otherwise, recycle it and keep going.
  243. * 2.) Search for a free partition that is large enough.
  244. * 3.) If there's not a free partition large enough, recycle any obsolete
  245. * OS partitions and try again.
  246. * 4.) Will first try getting a chunk that will satisfy the requested size.
  247. * 5.) If a chunk of the requested size cannot be allocated, then try finding
  248. * a chunk that will satisfy the minum needed.
  249. *
  250. * Returns 0 on success, else -1.
  251. */
  252. int __init nvram_init_os_partition(struct nvram_os_partition *part)
  253. {
  254. loff_t p;
  255. int size;
  256. /* Look for ours */
  257. p = nvram_find_partition(part->name, NVRAM_SIG_OS, &size);
  258. /* Found one but too small, remove it */
  259. if (p && size < part->min_size) {
  260. pr_info("nvram: Found too small %s partition,"
  261. " removing it...\n", part->name);
  262. nvram_remove_partition(part->name, NVRAM_SIG_OS, NULL);
  263. p = 0;
  264. }
  265. /* Create one if we didn't find */
  266. if (!p) {
  267. p = nvram_create_partition(part->name, NVRAM_SIG_OS,
  268. part->req_size, part->min_size);
  269. if (p == -ENOSPC) {
  270. pr_info("nvram: No room to create %s partition, "
  271. "deleting any obsolete OS partitions...\n",
  272. part->name);
  273. nvram_remove_partition(NULL, NVRAM_SIG_OS,
  274. nvram_os_partitions);
  275. p = nvram_create_partition(part->name, NVRAM_SIG_OS,
  276. part->req_size, part->min_size);
  277. }
  278. }
  279. if (p <= 0) {
  280. pr_err("nvram: Failed to find or create %s"
  281. " partition, err %d\n", part->name, (int)p);
  282. return -1;
  283. }
  284. part->index = p;
  285. part->size = nvram_get_partition_size(p) - sizeof(struct err_log_info);
  286. return 0;
  287. }
  288. /* Derived from logfs_compress() */
  289. static int nvram_compress(const void *in, void *out, size_t inlen,
  290. size_t outlen)
  291. {
  292. int err, ret;
  293. ret = -EIO;
  294. err = zlib_deflateInit2(&stream, COMPR_LEVEL, Z_DEFLATED, WINDOW_BITS,
  295. MEM_LEVEL, Z_DEFAULT_STRATEGY);
  296. if (err != Z_OK)
  297. goto error;
  298. stream.next_in = in;
  299. stream.avail_in = inlen;
  300. stream.total_in = 0;
  301. stream.next_out = out;
  302. stream.avail_out = outlen;
  303. stream.total_out = 0;
  304. err = zlib_deflate(&stream, Z_FINISH);
  305. if (err != Z_STREAM_END)
  306. goto error;
  307. err = zlib_deflateEnd(&stream);
  308. if (err != Z_OK)
  309. goto error;
  310. if (stream.total_out >= stream.total_in)
  311. goto error;
  312. ret = stream.total_out;
  313. error:
  314. return ret;
  315. }
  316. /* Compress the text from big_oops_buf into oops_buf. */
  317. static int zip_oops(size_t text_len)
  318. {
  319. struct oops_log_info *oops_hdr = (struct oops_log_info *)oops_buf;
  320. int zipped_len = nvram_compress(big_oops_buf, oops_data, text_len,
  321. oops_data_sz);
  322. if (zipped_len < 0) {
  323. pr_err("nvram: compression failed; returned %d\n", zipped_len);
  324. pr_err("nvram: logging uncompressed oops/panic report\n");
  325. return -1;
  326. }
  327. oops_hdr->version = cpu_to_be16(OOPS_HDR_VERSION);
  328. oops_hdr->report_length = cpu_to_be16(zipped_len);
  329. oops_hdr->timestamp = cpu_to_be64(ktime_get_real_seconds());
  330. return 0;
  331. }
  332. #ifdef CONFIG_PSTORE
  333. static int nvram_pstore_open(struct pstore_info *psi)
  334. {
  335. /* Reset the iterator to start reading partitions again */
  336. read_type = -1;
  337. return 0;
  338. }
  339. /**
  340. * nvram_pstore_write - pstore write callback for nvram
  341. * @type: Type of message logged
  342. * @reason: reason behind dump (oops/panic)
  343. * @id: identifier to indicate the write performed
  344. * @part: pstore writes data to registered buffer in parts,
  345. * part number will indicate the same.
  346. * @count: Indicates oops count
  347. * @compressed: Flag to indicate the log is compressed
  348. * @size: number of bytes written to the registered buffer
  349. * @psi: registered pstore_info structure
  350. *
  351. * Called by pstore_dump() when an oops or panic report is logged in the
  352. * printk buffer.
  353. * Returns 0 on successful write.
  354. */
  355. static int nvram_pstore_write(enum pstore_type_id type,
  356. enum kmsg_dump_reason reason,
  357. u64 *id, unsigned int part, int count,
  358. bool compressed, size_t size,
  359. struct pstore_info *psi)
  360. {
  361. int rc;
  362. unsigned int err_type = ERR_TYPE_KERNEL_PANIC;
  363. struct oops_log_info *oops_hdr = (struct oops_log_info *) oops_buf;
  364. /* part 1 has the recent messages from printk buffer */
  365. if (part > 1 || (type != PSTORE_TYPE_DMESG))
  366. return -1;
  367. if (clobbering_unread_rtas_event())
  368. return -1;
  369. oops_hdr->version = cpu_to_be16(OOPS_HDR_VERSION);
  370. oops_hdr->report_length = cpu_to_be16(size);
  371. oops_hdr->timestamp = cpu_to_be64(ktime_get_real_seconds());
  372. if (compressed)
  373. err_type = ERR_TYPE_KERNEL_PANIC_GZ;
  374. rc = nvram_write_os_partition(&oops_log_partition, oops_buf,
  375. (int) (sizeof(*oops_hdr) + size), err_type, count);
  376. if (rc != 0)
  377. return rc;
  378. *id = part;
  379. return 0;
  380. }
  381. /*
  382. * Reads the oops/panic report, rtas, of-config and common partition.
  383. * Returns the length of the data we read from each partition.
  384. * Returns 0 if we've been called before.
  385. */
  386. static ssize_t nvram_pstore_read(u64 *id, enum pstore_type_id *type,
  387. int *count, struct timespec *time, char **buf,
  388. bool *compressed, struct pstore_info *psi)
  389. {
  390. struct oops_log_info *oops_hdr;
  391. unsigned int err_type, id_no, size = 0;
  392. struct nvram_os_partition *part = NULL;
  393. char *buff = NULL;
  394. int sig = 0;
  395. loff_t p;
  396. read_type++;
  397. switch (nvram_type_ids[read_type]) {
  398. case PSTORE_TYPE_DMESG:
  399. part = &oops_log_partition;
  400. *type = PSTORE_TYPE_DMESG;
  401. break;
  402. case PSTORE_TYPE_PPC_COMMON:
  403. sig = NVRAM_SIG_SYS;
  404. part = &common_partition;
  405. *type = PSTORE_TYPE_PPC_COMMON;
  406. *id = PSTORE_TYPE_PPC_COMMON;
  407. time->tv_sec = 0;
  408. time->tv_nsec = 0;
  409. break;
  410. #ifdef CONFIG_PPC_PSERIES
  411. case PSTORE_TYPE_PPC_RTAS:
  412. part = &rtas_log_partition;
  413. *type = PSTORE_TYPE_PPC_RTAS;
  414. time->tv_sec = last_rtas_event;
  415. time->tv_nsec = 0;
  416. break;
  417. case PSTORE_TYPE_PPC_OF:
  418. sig = NVRAM_SIG_OF;
  419. part = &of_config_partition;
  420. *type = PSTORE_TYPE_PPC_OF;
  421. *id = PSTORE_TYPE_PPC_OF;
  422. time->tv_sec = 0;
  423. time->tv_nsec = 0;
  424. break;
  425. #endif
  426. #ifdef CONFIG_PPC_POWERNV
  427. case PSTORE_TYPE_PPC_OPAL:
  428. sig = NVRAM_SIG_FW;
  429. part = &skiboot_partition;
  430. *type = PSTORE_TYPE_PPC_OPAL;
  431. *id = PSTORE_TYPE_PPC_OPAL;
  432. time->tv_sec = 0;
  433. time->tv_nsec = 0;
  434. break;
  435. #endif
  436. default:
  437. return 0;
  438. }
  439. if (!part->os_partition) {
  440. p = nvram_find_partition(part->name, sig, &size);
  441. if (p <= 0) {
  442. pr_err("nvram: Failed to find partition %s, "
  443. "err %d\n", part->name, (int)p);
  444. return 0;
  445. }
  446. part->index = p;
  447. part->size = size;
  448. }
  449. buff = kmalloc(part->size, GFP_KERNEL);
  450. if (!buff)
  451. return -ENOMEM;
  452. if (nvram_read_partition(part, buff, part->size, &err_type, &id_no)) {
  453. kfree(buff);
  454. return 0;
  455. }
  456. *count = 0;
  457. if (part->os_partition)
  458. *id = id_no;
  459. if (nvram_type_ids[read_type] == PSTORE_TYPE_DMESG) {
  460. size_t length, hdr_size;
  461. oops_hdr = (struct oops_log_info *)buff;
  462. if (be16_to_cpu(oops_hdr->version) < OOPS_HDR_VERSION) {
  463. /* Old format oops header had 2-byte record size */
  464. hdr_size = sizeof(u16);
  465. length = be16_to_cpu(oops_hdr->version);
  466. time->tv_sec = 0;
  467. time->tv_nsec = 0;
  468. } else {
  469. hdr_size = sizeof(*oops_hdr);
  470. length = be16_to_cpu(oops_hdr->report_length);
  471. time->tv_sec = be64_to_cpu(oops_hdr->timestamp);
  472. time->tv_nsec = 0;
  473. }
  474. *buf = kmemdup(buff + hdr_size, length, GFP_KERNEL);
  475. if (*buf == NULL)
  476. return -ENOMEM;
  477. kfree(buff);
  478. if (err_type == ERR_TYPE_KERNEL_PANIC_GZ)
  479. *compressed = true;
  480. else
  481. *compressed = false;
  482. return length;
  483. }
  484. *buf = buff;
  485. return part->size;
  486. }
  487. static struct pstore_info nvram_pstore_info = {
  488. .owner = THIS_MODULE,
  489. .name = "nvram",
  490. .open = nvram_pstore_open,
  491. .read = nvram_pstore_read,
  492. .write = nvram_pstore_write,
  493. };
  494. static int nvram_pstore_init(void)
  495. {
  496. int rc = 0;
  497. if (machine_is(pseries)) {
  498. nvram_type_ids[2] = PSTORE_TYPE_PPC_RTAS;
  499. nvram_type_ids[3] = PSTORE_TYPE_PPC_OF;
  500. } else
  501. nvram_type_ids[2] = PSTORE_TYPE_PPC_OPAL;
  502. nvram_pstore_info.buf = oops_data;
  503. nvram_pstore_info.bufsize = oops_data_sz;
  504. spin_lock_init(&nvram_pstore_info.buf_lock);
  505. rc = pstore_register(&nvram_pstore_info);
  506. if (rc && (rc != -EPERM))
  507. /* Print error only when pstore.backend == nvram */
  508. pr_err("nvram: pstore_register() failed, returned %d. "
  509. "Defaults to kmsg_dump\n", rc);
  510. return rc;
  511. }
  512. #else
  513. static int nvram_pstore_init(void)
  514. {
  515. return -1;
  516. }
  517. #endif
  518. void __init nvram_init_oops_partition(int rtas_partition_exists)
  519. {
  520. int rc;
  521. rc = nvram_init_os_partition(&oops_log_partition);
  522. if (rc != 0) {
  523. #ifdef CONFIG_PPC_PSERIES
  524. if (!rtas_partition_exists) {
  525. pr_err("nvram: Failed to initialize oops partition!");
  526. return;
  527. }
  528. pr_notice("nvram: Using %s partition to log both"
  529. " RTAS errors and oops/panic reports\n",
  530. rtas_log_partition.name);
  531. memcpy(&oops_log_partition, &rtas_log_partition,
  532. sizeof(rtas_log_partition));
  533. #else
  534. pr_err("nvram: Failed to initialize oops partition!");
  535. return;
  536. #endif
  537. }
  538. oops_buf = kmalloc(oops_log_partition.size, GFP_KERNEL);
  539. if (!oops_buf) {
  540. pr_err("nvram: No memory for %s partition\n",
  541. oops_log_partition.name);
  542. return;
  543. }
  544. oops_data = oops_buf + sizeof(struct oops_log_info);
  545. oops_data_sz = oops_log_partition.size - sizeof(struct oops_log_info);
  546. rc = nvram_pstore_init();
  547. if (!rc)
  548. return;
  549. /*
  550. * Figure compression (preceded by elimination of each line's <n>
  551. * severity prefix) will reduce the oops/panic report to at most
  552. * 45% of its original size.
  553. */
  554. big_oops_buf_sz = (oops_data_sz * 100) / 45;
  555. big_oops_buf = kmalloc(big_oops_buf_sz, GFP_KERNEL);
  556. if (big_oops_buf) {
  557. stream.workspace = kmalloc(zlib_deflate_workspacesize(
  558. WINDOW_BITS, MEM_LEVEL), GFP_KERNEL);
  559. if (!stream.workspace) {
  560. pr_err("nvram: No memory for compression workspace; "
  561. "skipping compression of %s partition data\n",
  562. oops_log_partition.name);
  563. kfree(big_oops_buf);
  564. big_oops_buf = NULL;
  565. }
  566. } else {
  567. pr_err("No memory for uncompressed %s data; "
  568. "skipping compression\n", oops_log_partition.name);
  569. stream.workspace = NULL;
  570. }
  571. rc = kmsg_dump_register(&nvram_kmsg_dumper);
  572. if (rc != 0) {
  573. pr_err("nvram: kmsg_dump_register() failed; returned %d\n", rc);
  574. kfree(oops_buf);
  575. kfree(big_oops_buf);
  576. kfree(stream.workspace);
  577. }
  578. }
  579. /*
  580. * This is our kmsg_dump callback, called after an oops or panic report
  581. * has been written to the printk buffer. We want to capture as much
  582. * of the printk buffer as possible. First, capture as much as we can
  583. * that we think will compress sufficiently to fit in the lnx,oops-log
  584. * partition. If that's too much, go back and capture uncompressed text.
  585. */
  586. static void oops_to_nvram(struct kmsg_dumper *dumper,
  587. enum kmsg_dump_reason reason)
  588. {
  589. struct oops_log_info *oops_hdr = (struct oops_log_info *)oops_buf;
  590. static unsigned int oops_count = 0;
  591. static bool panicking = false;
  592. static DEFINE_SPINLOCK(lock);
  593. unsigned long flags;
  594. size_t text_len;
  595. unsigned int err_type = ERR_TYPE_KERNEL_PANIC_GZ;
  596. int rc = -1;
  597. switch (reason) {
  598. case KMSG_DUMP_RESTART:
  599. case KMSG_DUMP_HALT:
  600. case KMSG_DUMP_POWEROFF:
  601. /* These are almost always orderly shutdowns. */
  602. return;
  603. case KMSG_DUMP_OOPS:
  604. break;
  605. case KMSG_DUMP_PANIC:
  606. panicking = true;
  607. break;
  608. case KMSG_DUMP_EMERG:
  609. if (panicking)
  610. /* Panic report already captured. */
  611. return;
  612. break;
  613. default:
  614. pr_err("%s: ignoring unrecognized KMSG_DUMP_* reason %d\n",
  615. __func__, (int) reason);
  616. return;
  617. }
  618. if (clobbering_unread_rtas_event())
  619. return;
  620. if (!spin_trylock_irqsave(&lock, flags))
  621. return;
  622. if (big_oops_buf) {
  623. kmsg_dump_get_buffer(dumper, false,
  624. big_oops_buf, big_oops_buf_sz, &text_len);
  625. rc = zip_oops(text_len);
  626. }
  627. if (rc != 0) {
  628. kmsg_dump_rewind(dumper);
  629. kmsg_dump_get_buffer(dumper, false,
  630. oops_data, oops_data_sz, &text_len);
  631. err_type = ERR_TYPE_KERNEL_PANIC;
  632. oops_hdr->version = cpu_to_be16(OOPS_HDR_VERSION);
  633. oops_hdr->report_length = cpu_to_be16(text_len);
  634. oops_hdr->timestamp = cpu_to_be64(ktime_get_real_seconds());
  635. }
  636. (void) nvram_write_os_partition(&oops_log_partition, oops_buf,
  637. (int) (sizeof(*oops_hdr) + text_len), err_type,
  638. ++oops_count);
  639. spin_unlock_irqrestore(&lock, flags);
  640. }
  641. static loff_t dev_nvram_llseek(struct file *file, loff_t offset, int origin)
  642. {
  643. int size;
  644. if (ppc_md.nvram_size == NULL)
  645. return -ENODEV;
  646. size = ppc_md.nvram_size();
  647. switch (origin) {
  648. case 1:
  649. offset += file->f_pos;
  650. break;
  651. case 2:
  652. offset += size;
  653. break;
  654. }
  655. if (offset < 0)
  656. return -EINVAL;
  657. file->f_pos = offset;
  658. return file->f_pos;
  659. }
  660. static ssize_t dev_nvram_read(struct file *file, char __user *buf,
  661. size_t count, loff_t *ppos)
  662. {
  663. ssize_t ret;
  664. char *tmp = NULL;
  665. ssize_t size;
  666. if (!ppc_md.nvram_size) {
  667. ret = -ENODEV;
  668. goto out;
  669. }
  670. size = ppc_md.nvram_size();
  671. if (size < 0) {
  672. ret = size;
  673. goto out;
  674. }
  675. if (*ppos >= size) {
  676. ret = 0;
  677. goto out;
  678. }
  679. count = min_t(size_t, count, size - *ppos);
  680. count = min(count, PAGE_SIZE);
  681. tmp = kmalloc(count, GFP_KERNEL);
  682. if (!tmp) {
  683. ret = -ENOMEM;
  684. goto out;
  685. }
  686. ret = ppc_md.nvram_read(tmp, count, ppos);
  687. if (ret <= 0)
  688. goto out;
  689. if (copy_to_user(buf, tmp, ret))
  690. ret = -EFAULT;
  691. out:
  692. kfree(tmp);
  693. return ret;
  694. }
  695. static ssize_t dev_nvram_write(struct file *file, const char __user *buf,
  696. size_t count, loff_t *ppos)
  697. {
  698. ssize_t ret;
  699. char *tmp = NULL;
  700. ssize_t size;
  701. ret = -ENODEV;
  702. if (!ppc_md.nvram_size)
  703. goto out;
  704. ret = 0;
  705. size = ppc_md.nvram_size();
  706. if (*ppos >= size || size < 0)
  707. goto out;
  708. count = min_t(size_t, count, size - *ppos);
  709. count = min(count, PAGE_SIZE);
  710. ret = -ENOMEM;
  711. tmp = kmalloc(count, GFP_KERNEL);
  712. if (!tmp)
  713. goto out;
  714. ret = -EFAULT;
  715. if (copy_from_user(tmp, buf, count))
  716. goto out;
  717. ret = ppc_md.nvram_write(tmp, count, ppos);
  718. out:
  719. kfree(tmp);
  720. return ret;
  721. }
  722. static long dev_nvram_ioctl(struct file *file, unsigned int cmd,
  723. unsigned long arg)
  724. {
  725. switch(cmd) {
  726. #ifdef CONFIG_PPC_PMAC
  727. case OBSOLETE_PMAC_NVRAM_GET_OFFSET:
  728. printk(KERN_WARNING "nvram: Using obsolete PMAC_NVRAM_GET_OFFSET ioctl\n");
  729. case IOC_NVRAM_GET_OFFSET: {
  730. int part, offset;
  731. if (!machine_is(powermac))
  732. return -EINVAL;
  733. if (copy_from_user(&part, (void __user*)arg, sizeof(part)) != 0)
  734. return -EFAULT;
  735. if (part < pmac_nvram_OF || part > pmac_nvram_NR)
  736. return -EINVAL;
  737. offset = pmac_get_partition(part);
  738. if (offset < 0)
  739. return offset;
  740. if (copy_to_user((void __user*)arg, &offset, sizeof(offset)) != 0)
  741. return -EFAULT;
  742. return 0;
  743. }
  744. #endif /* CONFIG_PPC_PMAC */
  745. default:
  746. return -EINVAL;
  747. }
  748. }
  749. const struct file_operations nvram_fops = {
  750. .owner = THIS_MODULE,
  751. .llseek = dev_nvram_llseek,
  752. .read = dev_nvram_read,
  753. .write = dev_nvram_write,
  754. .unlocked_ioctl = dev_nvram_ioctl,
  755. };
  756. static struct miscdevice nvram_dev = {
  757. NVRAM_MINOR,
  758. "nvram",
  759. &nvram_fops
  760. };
  761. #ifdef DEBUG_NVRAM
  762. static void __init nvram_print_partitions(char * label)
  763. {
  764. struct nvram_partition * tmp_part;
  765. printk(KERN_WARNING "--------%s---------\n", label);
  766. printk(KERN_WARNING "indx\t\tsig\tchks\tlen\tname\n");
  767. list_for_each_entry(tmp_part, &nvram_partitions, partition) {
  768. printk(KERN_WARNING "%4d \t%02x\t%02x\t%d\t%12.12s\n",
  769. tmp_part->index, tmp_part->header.signature,
  770. tmp_part->header.checksum, tmp_part->header.length,
  771. tmp_part->header.name);
  772. }
  773. }
  774. #endif
  775. static int __init nvram_write_header(struct nvram_partition * part)
  776. {
  777. loff_t tmp_index;
  778. int rc;
  779. struct nvram_header phead;
  780. memcpy(&phead, &part->header, NVRAM_HEADER_LEN);
  781. phead.length = cpu_to_be16(phead.length);
  782. tmp_index = part->index;
  783. rc = ppc_md.nvram_write((char *)&phead, NVRAM_HEADER_LEN, &tmp_index);
  784. return rc;
  785. }
  786. static unsigned char __init nvram_checksum(struct nvram_header *p)
  787. {
  788. unsigned int c_sum, c_sum2;
  789. unsigned short *sp = (unsigned short *)p->name; /* assume 6 shorts */
  790. c_sum = p->signature + p->length + sp[0] + sp[1] + sp[2] + sp[3] + sp[4] + sp[5];
  791. /* The sum may have spilled into the 3rd byte. Fold it back. */
  792. c_sum = ((c_sum & 0xffff) + (c_sum >> 16)) & 0xffff;
  793. /* The sum cannot exceed 2 bytes. Fold it into a checksum */
  794. c_sum2 = (c_sum >> 8) + (c_sum << 8);
  795. c_sum = ((c_sum + c_sum2) >> 8) & 0xff;
  796. return c_sum;
  797. }
  798. /*
  799. * Per the criteria passed via nvram_remove_partition(), should this
  800. * partition be removed? 1=remove, 0=keep
  801. */
  802. static int nvram_can_remove_partition(struct nvram_partition *part,
  803. const char *name, int sig, const char *exceptions[])
  804. {
  805. if (part->header.signature != sig)
  806. return 0;
  807. if (name) {
  808. if (strncmp(name, part->header.name, 12))
  809. return 0;
  810. } else if (exceptions) {
  811. const char **except;
  812. for (except = exceptions; *except; except++) {
  813. if (!strncmp(*except, part->header.name, 12))
  814. return 0;
  815. }
  816. }
  817. return 1;
  818. }
  819. /**
  820. * nvram_remove_partition - Remove one or more partitions in nvram
  821. * @name: name of the partition to remove, or NULL for a
  822. * signature only match
  823. * @sig: signature of the partition(s) to remove
  824. * @exceptions: When removing all partitions with a matching signature,
  825. * leave these alone.
  826. */
  827. int __init nvram_remove_partition(const char *name, int sig,
  828. const char *exceptions[])
  829. {
  830. struct nvram_partition *part, *prev, *tmp;
  831. int rc;
  832. list_for_each_entry(part, &nvram_partitions, partition) {
  833. if (!nvram_can_remove_partition(part, name, sig, exceptions))
  834. continue;
  835. /* Make partition a free partition */
  836. part->header.signature = NVRAM_SIG_FREE;
  837. strncpy(part->header.name, "wwwwwwwwwwww", 12);
  838. part->header.checksum = nvram_checksum(&part->header);
  839. rc = nvram_write_header(part);
  840. if (rc <= 0) {
  841. printk(KERN_ERR "nvram_remove_partition: nvram_write failed (%d)\n", rc);
  842. return rc;
  843. }
  844. }
  845. /* Merge contiguous ones */
  846. prev = NULL;
  847. list_for_each_entry_safe(part, tmp, &nvram_partitions, partition) {
  848. if (part->header.signature != NVRAM_SIG_FREE) {
  849. prev = NULL;
  850. continue;
  851. }
  852. if (prev) {
  853. prev->header.length += part->header.length;
  854. prev->header.checksum = nvram_checksum(&part->header);
  855. rc = nvram_write_header(part);
  856. if (rc <= 0) {
  857. printk(KERN_ERR "nvram_remove_partition: nvram_write failed (%d)\n", rc);
  858. return rc;
  859. }
  860. list_del(&part->partition);
  861. kfree(part);
  862. } else
  863. prev = part;
  864. }
  865. return 0;
  866. }
  867. /**
  868. * nvram_create_partition - Create a partition in nvram
  869. * @name: name of the partition to create
  870. * @sig: signature of the partition to create
  871. * @req_size: size of data to allocate in bytes
  872. * @min_size: minimum acceptable size (0 means req_size)
  873. *
  874. * Returns a negative error code or a positive nvram index
  875. * of the beginning of the data area of the newly created
  876. * partition. If you provided a min_size smaller than req_size
  877. * you need to query for the actual size yourself after the
  878. * call using nvram_partition_get_size().
  879. */
  880. loff_t __init nvram_create_partition(const char *name, int sig,
  881. int req_size, int min_size)
  882. {
  883. struct nvram_partition *part;
  884. struct nvram_partition *new_part;
  885. struct nvram_partition *free_part = NULL;
  886. static char nv_init_vals[16];
  887. loff_t tmp_index;
  888. long size = 0;
  889. int rc;
  890. /* Convert sizes from bytes to blocks */
  891. req_size = _ALIGN_UP(req_size, NVRAM_BLOCK_LEN) / NVRAM_BLOCK_LEN;
  892. min_size = _ALIGN_UP(min_size, NVRAM_BLOCK_LEN) / NVRAM_BLOCK_LEN;
  893. /* If no minimum size specified, make it the same as the
  894. * requested size
  895. */
  896. if (min_size == 0)
  897. min_size = req_size;
  898. if (min_size > req_size)
  899. return -EINVAL;
  900. /* Now add one block to each for the header */
  901. req_size += 1;
  902. min_size += 1;
  903. /* Find a free partition that will give us the maximum needed size
  904. If can't find one that will give us the minimum size needed */
  905. list_for_each_entry(part, &nvram_partitions, partition) {
  906. if (part->header.signature != NVRAM_SIG_FREE)
  907. continue;
  908. if (part->header.length >= req_size) {
  909. size = req_size;
  910. free_part = part;
  911. break;
  912. }
  913. if (part->header.length > size &&
  914. part->header.length >= min_size) {
  915. size = part->header.length;
  916. free_part = part;
  917. }
  918. }
  919. if (!size)
  920. return -ENOSPC;
  921. /* Create our OS partition */
  922. new_part = kmalloc(sizeof(*new_part), GFP_KERNEL);
  923. if (!new_part) {
  924. pr_err("nvram_create_os_partition: kmalloc failed\n");
  925. return -ENOMEM;
  926. }
  927. new_part->index = free_part->index;
  928. new_part->header.signature = sig;
  929. new_part->header.length = size;
  930. strncpy(new_part->header.name, name, 12);
  931. new_part->header.checksum = nvram_checksum(&new_part->header);
  932. rc = nvram_write_header(new_part);
  933. if (rc <= 0) {
  934. pr_err("nvram_create_os_partition: nvram_write_header "
  935. "failed (%d)\n", rc);
  936. return rc;
  937. }
  938. list_add_tail(&new_part->partition, &free_part->partition);
  939. /* Adjust or remove the partition we stole the space from */
  940. if (free_part->header.length > size) {
  941. free_part->index += size * NVRAM_BLOCK_LEN;
  942. free_part->header.length -= size;
  943. free_part->header.checksum = nvram_checksum(&free_part->header);
  944. rc = nvram_write_header(free_part);
  945. if (rc <= 0) {
  946. pr_err("nvram_create_os_partition: nvram_write_header "
  947. "failed (%d)\n", rc);
  948. return rc;
  949. }
  950. } else {
  951. list_del(&free_part->partition);
  952. kfree(free_part);
  953. }
  954. /* Clear the new partition */
  955. for (tmp_index = new_part->index + NVRAM_HEADER_LEN;
  956. tmp_index < ((size - 1) * NVRAM_BLOCK_LEN);
  957. tmp_index += NVRAM_BLOCK_LEN) {
  958. rc = ppc_md.nvram_write(nv_init_vals, NVRAM_BLOCK_LEN, &tmp_index);
  959. if (rc <= 0) {
  960. pr_err("nvram_create_partition: nvram_write failed (%d)\n", rc);
  961. return rc;
  962. }
  963. }
  964. return new_part->index + NVRAM_HEADER_LEN;
  965. }
  966. /**
  967. * nvram_get_partition_size - Get the data size of an nvram partition
  968. * @data_index: This is the offset of the start of the data of
  969. * the partition. The same value that is returned by
  970. * nvram_create_partition().
  971. */
  972. int nvram_get_partition_size(loff_t data_index)
  973. {
  974. struct nvram_partition *part;
  975. list_for_each_entry(part, &nvram_partitions, partition) {
  976. if (part->index + NVRAM_HEADER_LEN == data_index)
  977. return (part->header.length - 1) * NVRAM_BLOCK_LEN;
  978. }
  979. return -1;
  980. }
  981. /**
  982. * nvram_find_partition - Find an nvram partition by signature and name
  983. * @name: Name of the partition or NULL for any name
  984. * @sig: Signature to test against
  985. * @out_size: if non-NULL, returns the size of the data part of the partition
  986. */
  987. loff_t nvram_find_partition(const char *name, int sig, int *out_size)
  988. {
  989. struct nvram_partition *p;
  990. list_for_each_entry(p, &nvram_partitions, partition) {
  991. if (p->header.signature == sig &&
  992. (!name || !strncmp(p->header.name, name, 12))) {
  993. if (out_size)
  994. *out_size = (p->header.length - 1) *
  995. NVRAM_BLOCK_LEN;
  996. return p->index + NVRAM_HEADER_LEN;
  997. }
  998. }
  999. return 0;
  1000. }
  1001. int __init nvram_scan_partitions(void)
  1002. {
  1003. loff_t cur_index = 0;
  1004. struct nvram_header phead;
  1005. struct nvram_partition * tmp_part;
  1006. unsigned char c_sum;
  1007. char * header;
  1008. int total_size;
  1009. int err;
  1010. if (ppc_md.nvram_size == NULL || ppc_md.nvram_size() <= 0)
  1011. return -ENODEV;
  1012. total_size = ppc_md.nvram_size();
  1013. header = kmalloc(NVRAM_HEADER_LEN, GFP_KERNEL);
  1014. if (!header) {
  1015. printk(KERN_ERR "nvram_scan_partitions: Failed kmalloc\n");
  1016. return -ENOMEM;
  1017. }
  1018. while (cur_index < total_size) {
  1019. err = ppc_md.nvram_read(header, NVRAM_HEADER_LEN, &cur_index);
  1020. if (err != NVRAM_HEADER_LEN) {
  1021. printk(KERN_ERR "nvram_scan_partitions: Error parsing "
  1022. "nvram partitions\n");
  1023. goto out;
  1024. }
  1025. cur_index -= NVRAM_HEADER_LEN; /* nvram_read will advance us */
  1026. memcpy(&phead, header, NVRAM_HEADER_LEN);
  1027. phead.length = be16_to_cpu(phead.length);
  1028. err = 0;
  1029. c_sum = nvram_checksum(&phead);
  1030. if (c_sum != phead.checksum) {
  1031. printk(KERN_WARNING "WARNING: nvram partition checksum"
  1032. " was %02x, should be %02x!\n",
  1033. phead.checksum, c_sum);
  1034. printk(KERN_WARNING "Terminating nvram partition scan\n");
  1035. goto out;
  1036. }
  1037. if (!phead.length) {
  1038. printk(KERN_WARNING "WARNING: nvram corruption "
  1039. "detected: 0-length partition\n");
  1040. goto out;
  1041. }
  1042. tmp_part = kmalloc(sizeof(struct nvram_partition), GFP_KERNEL);
  1043. err = -ENOMEM;
  1044. if (!tmp_part) {
  1045. printk(KERN_ERR "nvram_scan_partitions: kmalloc failed\n");
  1046. goto out;
  1047. }
  1048. memcpy(&tmp_part->header, &phead, NVRAM_HEADER_LEN);
  1049. tmp_part->index = cur_index;
  1050. list_add_tail(&tmp_part->partition, &nvram_partitions);
  1051. cur_index += phead.length * NVRAM_BLOCK_LEN;
  1052. }
  1053. err = 0;
  1054. #ifdef DEBUG_NVRAM
  1055. nvram_print_partitions("NVRAM Partitions");
  1056. #endif
  1057. out:
  1058. kfree(header);
  1059. return err;
  1060. }
  1061. static int __init nvram_init(void)
  1062. {
  1063. int rc;
  1064. BUILD_BUG_ON(NVRAM_BLOCK_LEN != 16);
  1065. if (ppc_md.nvram_size == NULL || ppc_md.nvram_size() <= 0)
  1066. return -ENODEV;
  1067. rc = misc_register(&nvram_dev);
  1068. if (rc != 0) {
  1069. printk(KERN_ERR "nvram_init: failed to register device\n");
  1070. return rc;
  1071. }
  1072. return rc;
  1073. }
  1074. static void __exit nvram_cleanup(void)
  1075. {
  1076. misc_deregister( &nvram_dev );
  1077. }
  1078. module_init(nvram_init);
  1079. module_exit(nvram_cleanup);
  1080. MODULE_LICENSE("GPL");