nvram_64.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234
  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/types.h>
  18. #include <linux/errno.h>
  19. #include <linux/fs.h>
  20. #include <linux/miscdevice.h>
  21. #include <linux/fcntl.h>
  22. #include <linux/nvram.h>
  23. #include <linux/init.h>
  24. #include <linux/slab.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/kmsg_dump.h>
  27. #include <linux/pagemap.h>
  28. #include <linux/pstore.h>
  29. #include <linux/zlib.h>
  30. #include <linux/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, ssize_t *ecc_notice_size,
  389. struct pstore_info *psi)
  390. {
  391. struct oops_log_info *oops_hdr;
  392. unsigned int err_type, id_no, size = 0;
  393. struct nvram_os_partition *part = NULL;
  394. char *buff = NULL;
  395. int sig = 0;
  396. loff_t p;
  397. read_type++;
  398. switch (nvram_type_ids[read_type]) {
  399. case PSTORE_TYPE_DMESG:
  400. part = &oops_log_partition;
  401. *type = PSTORE_TYPE_DMESG;
  402. break;
  403. case PSTORE_TYPE_PPC_COMMON:
  404. sig = NVRAM_SIG_SYS;
  405. part = &common_partition;
  406. *type = PSTORE_TYPE_PPC_COMMON;
  407. *id = PSTORE_TYPE_PPC_COMMON;
  408. time->tv_sec = 0;
  409. time->tv_nsec = 0;
  410. break;
  411. #ifdef CONFIG_PPC_PSERIES
  412. case PSTORE_TYPE_PPC_RTAS:
  413. part = &rtas_log_partition;
  414. *type = PSTORE_TYPE_PPC_RTAS;
  415. time->tv_sec = last_rtas_event;
  416. time->tv_nsec = 0;
  417. break;
  418. case PSTORE_TYPE_PPC_OF:
  419. sig = NVRAM_SIG_OF;
  420. part = &of_config_partition;
  421. *type = PSTORE_TYPE_PPC_OF;
  422. *id = PSTORE_TYPE_PPC_OF;
  423. time->tv_sec = 0;
  424. time->tv_nsec = 0;
  425. break;
  426. #endif
  427. #ifdef CONFIG_PPC_POWERNV
  428. case PSTORE_TYPE_PPC_OPAL:
  429. sig = NVRAM_SIG_FW;
  430. part = &skiboot_partition;
  431. *type = PSTORE_TYPE_PPC_OPAL;
  432. *id = PSTORE_TYPE_PPC_OPAL;
  433. time->tv_sec = 0;
  434. time->tv_nsec = 0;
  435. break;
  436. #endif
  437. default:
  438. return 0;
  439. }
  440. if (!part->os_partition) {
  441. p = nvram_find_partition(part->name, sig, &size);
  442. if (p <= 0) {
  443. pr_err("nvram: Failed to find partition %s, "
  444. "err %d\n", part->name, (int)p);
  445. return 0;
  446. }
  447. part->index = p;
  448. part->size = size;
  449. }
  450. buff = kmalloc(part->size, GFP_KERNEL);
  451. if (!buff)
  452. return -ENOMEM;
  453. if (nvram_read_partition(part, buff, part->size, &err_type, &id_no)) {
  454. kfree(buff);
  455. return 0;
  456. }
  457. *count = 0;
  458. if (part->os_partition)
  459. *id = id_no;
  460. if (nvram_type_ids[read_type] == PSTORE_TYPE_DMESG) {
  461. size_t length, hdr_size;
  462. oops_hdr = (struct oops_log_info *)buff;
  463. if (be16_to_cpu(oops_hdr->version) < OOPS_HDR_VERSION) {
  464. /* Old format oops header had 2-byte record size */
  465. hdr_size = sizeof(u16);
  466. length = be16_to_cpu(oops_hdr->version);
  467. time->tv_sec = 0;
  468. time->tv_nsec = 0;
  469. } else {
  470. hdr_size = sizeof(*oops_hdr);
  471. length = be16_to_cpu(oops_hdr->report_length);
  472. time->tv_sec = be64_to_cpu(oops_hdr->timestamp);
  473. time->tv_nsec = 0;
  474. }
  475. *buf = kmemdup(buff + hdr_size, length, GFP_KERNEL);
  476. kfree(buff);
  477. if (*buf == NULL)
  478. return -ENOMEM;
  479. *ecc_notice_size = 0;
  480. if (err_type == ERR_TYPE_KERNEL_PANIC_GZ)
  481. *compressed = true;
  482. else
  483. *compressed = false;
  484. return length;
  485. }
  486. *buf = buff;
  487. return part->size;
  488. }
  489. static struct pstore_info nvram_pstore_info = {
  490. .owner = THIS_MODULE,
  491. .name = "nvram",
  492. .open = nvram_pstore_open,
  493. .read = nvram_pstore_read,
  494. .write = nvram_pstore_write,
  495. };
  496. static int nvram_pstore_init(void)
  497. {
  498. int rc = 0;
  499. if (machine_is(pseries)) {
  500. nvram_type_ids[2] = PSTORE_TYPE_PPC_RTAS;
  501. nvram_type_ids[3] = PSTORE_TYPE_PPC_OF;
  502. } else
  503. nvram_type_ids[2] = PSTORE_TYPE_PPC_OPAL;
  504. nvram_pstore_info.buf = oops_data;
  505. nvram_pstore_info.bufsize = oops_data_sz;
  506. spin_lock_init(&nvram_pstore_info.buf_lock);
  507. rc = pstore_register(&nvram_pstore_info);
  508. if (rc && (rc != -EPERM))
  509. /* Print error only when pstore.backend == nvram */
  510. pr_err("nvram: pstore_register() failed, returned %d. "
  511. "Defaults to kmsg_dump\n", rc);
  512. return rc;
  513. }
  514. #else
  515. static int nvram_pstore_init(void)
  516. {
  517. return -1;
  518. }
  519. #endif
  520. void __init nvram_init_oops_partition(int rtas_partition_exists)
  521. {
  522. int rc;
  523. rc = nvram_init_os_partition(&oops_log_partition);
  524. if (rc != 0) {
  525. #ifdef CONFIG_PPC_PSERIES
  526. if (!rtas_partition_exists) {
  527. pr_err("nvram: Failed to initialize oops partition!");
  528. return;
  529. }
  530. pr_notice("nvram: Using %s partition to log both"
  531. " RTAS errors and oops/panic reports\n",
  532. rtas_log_partition.name);
  533. memcpy(&oops_log_partition, &rtas_log_partition,
  534. sizeof(rtas_log_partition));
  535. #else
  536. pr_err("nvram: Failed to initialize oops partition!");
  537. return;
  538. #endif
  539. }
  540. oops_buf = kmalloc(oops_log_partition.size, GFP_KERNEL);
  541. if (!oops_buf) {
  542. pr_err("nvram: No memory for %s partition\n",
  543. oops_log_partition.name);
  544. return;
  545. }
  546. oops_data = oops_buf + sizeof(struct oops_log_info);
  547. oops_data_sz = oops_log_partition.size - sizeof(struct oops_log_info);
  548. rc = nvram_pstore_init();
  549. if (!rc)
  550. return;
  551. /*
  552. * Figure compression (preceded by elimination of each line's <n>
  553. * severity prefix) will reduce the oops/panic report to at most
  554. * 45% of its original size.
  555. */
  556. big_oops_buf_sz = (oops_data_sz * 100) / 45;
  557. big_oops_buf = kmalloc(big_oops_buf_sz, GFP_KERNEL);
  558. if (big_oops_buf) {
  559. stream.workspace = kmalloc(zlib_deflate_workspacesize(
  560. WINDOW_BITS, MEM_LEVEL), GFP_KERNEL);
  561. if (!stream.workspace) {
  562. pr_err("nvram: No memory for compression workspace; "
  563. "skipping compression of %s partition data\n",
  564. oops_log_partition.name);
  565. kfree(big_oops_buf);
  566. big_oops_buf = NULL;
  567. }
  568. } else {
  569. pr_err("No memory for uncompressed %s data; "
  570. "skipping compression\n", oops_log_partition.name);
  571. stream.workspace = NULL;
  572. }
  573. rc = kmsg_dump_register(&nvram_kmsg_dumper);
  574. if (rc != 0) {
  575. pr_err("nvram: kmsg_dump_register() failed; returned %d\n", rc);
  576. kfree(oops_buf);
  577. kfree(big_oops_buf);
  578. kfree(stream.workspace);
  579. }
  580. }
  581. /*
  582. * This is our kmsg_dump callback, called after an oops or panic report
  583. * has been written to the printk buffer. We want to capture as much
  584. * of the printk buffer as possible. First, capture as much as we can
  585. * that we think will compress sufficiently to fit in the lnx,oops-log
  586. * partition. If that's too much, go back and capture uncompressed text.
  587. */
  588. static void oops_to_nvram(struct kmsg_dumper *dumper,
  589. enum kmsg_dump_reason reason)
  590. {
  591. struct oops_log_info *oops_hdr = (struct oops_log_info *)oops_buf;
  592. static unsigned int oops_count = 0;
  593. static bool panicking = false;
  594. static DEFINE_SPINLOCK(lock);
  595. unsigned long flags;
  596. size_t text_len;
  597. unsigned int err_type = ERR_TYPE_KERNEL_PANIC_GZ;
  598. int rc = -1;
  599. switch (reason) {
  600. case KMSG_DUMP_RESTART:
  601. case KMSG_DUMP_HALT:
  602. case KMSG_DUMP_POWEROFF:
  603. /* These are almost always orderly shutdowns. */
  604. return;
  605. case KMSG_DUMP_OOPS:
  606. break;
  607. case KMSG_DUMP_PANIC:
  608. panicking = true;
  609. break;
  610. case KMSG_DUMP_EMERG:
  611. if (panicking)
  612. /* Panic report already captured. */
  613. return;
  614. break;
  615. default:
  616. pr_err("%s: ignoring unrecognized KMSG_DUMP_* reason %d\n",
  617. __func__, (int) reason);
  618. return;
  619. }
  620. if (clobbering_unread_rtas_event())
  621. return;
  622. if (!spin_trylock_irqsave(&lock, flags))
  623. return;
  624. if (big_oops_buf) {
  625. kmsg_dump_get_buffer(dumper, false,
  626. big_oops_buf, big_oops_buf_sz, &text_len);
  627. rc = zip_oops(text_len);
  628. }
  629. if (rc != 0) {
  630. kmsg_dump_rewind(dumper);
  631. kmsg_dump_get_buffer(dumper, false,
  632. oops_data, oops_data_sz, &text_len);
  633. err_type = ERR_TYPE_KERNEL_PANIC;
  634. oops_hdr->version = cpu_to_be16(OOPS_HDR_VERSION);
  635. oops_hdr->report_length = cpu_to_be16(text_len);
  636. oops_hdr->timestamp = cpu_to_be64(ktime_get_real_seconds());
  637. }
  638. (void) nvram_write_os_partition(&oops_log_partition, oops_buf,
  639. (int) (sizeof(*oops_hdr) + text_len), err_type,
  640. ++oops_count);
  641. spin_unlock_irqrestore(&lock, flags);
  642. }
  643. static loff_t dev_nvram_llseek(struct file *file, loff_t offset, int origin)
  644. {
  645. if (ppc_md.nvram_size == NULL)
  646. return -ENODEV;
  647. return generic_file_llseek_size(file, offset, origin, MAX_LFS_FILESIZE,
  648. ppc_md.nvram_size());
  649. }
  650. static ssize_t dev_nvram_read(struct file *file, char __user *buf,
  651. size_t count, loff_t *ppos)
  652. {
  653. ssize_t ret;
  654. char *tmp = NULL;
  655. ssize_t size;
  656. if (!ppc_md.nvram_size) {
  657. ret = -ENODEV;
  658. goto out;
  659. }
  660. size = ppc_md.nvram_size();
  661. if (size < 0) {
  662. ret = size;
  663. goto out;
  664. }
  665. if (*ppos >= size) {
  666. ret = 0;
  667. goto out;
  668. }
  669. count = min_t(size_t, count, size - *ppos);
  670. count = min(count, PAGE_SIZE);
  671. tmp = kmalloc(count, GFP_KERNEL);
  672. if (!tmp) {
  673. ret = -ENOMEM;
  674. goto out;
  675. }
  676. ret = ppc_md.nvram_read(tmp, count, ppos);
  677. if (ret <= 0)
  678. goto out;
  679. if (copy_to_user(buf, tmp, ret))
  680. ret = -EFAULT;
  681. out:
  682. kfree(tmp);
  683. return ret;
  684. }
  685. static ssize_t dev_nvram_write(struct file *file, const char __user *buf,
  686. size_t count, loff_t *ppos)
  687. {
  688. ssize_t ret;
  689. char *tmp = NULL;
  690. ssize_t size;
  691. ret = -ENODEV;
  692. if (!ppc_md.nvram_size)
  693. goto out;
  694. ret = 0;
  695. size = ppc_md.nvram_size();
  696. if (*ppos >= size || size < 0)
  697. goto out;
  698. count = min_t(size_t, count, size - *ppos);
  699. count = min(count, PAGE_SIZE);
  700. ret = -ENOMEM;
  701. tmp = kmalloc(count, GFP_KERNEL);
  702. if (!tmp)
  703. goto out;
  704. ret = -EFAULT;
  705. if (copy_from_user(tmp, buf, count))
  706. goto out;
  707. ret = ppc_md.nvram_write(tmp, count, ppos);
  708. out:
  709. kfree(tmp);
  710. return ret;
  711. }
  712. static long dev_nvram_ioctl(struct file *file, unsigned int cmd,
  713. unsigned long arg)
  714. {
  715. switch(cmd) {
  716. #ifdef CONFIG_PPC_PMAC
  717. case OBSOLETE_PMAC_NVRAM_GET_OFFSET:
  718. printk(KERN_WARNING "nvram: Using obsolete PMAC_NVRAM_GET_OFFSET ioctl\n");
  719. case IOC_NVRAM_GET_OFFSET: {
  720. int part, offset;
  721. if (!machine_is(powermac))
  722. return -EINVAL;
  723. if (copy_from_user(&part, (void __user*)arg, sizeof(part)) != 0)
  724. return -EFAULT;
  725. if (part < pmac_nvram_OF || part > pmac_nvram_NR)
  726. return -EINVAL;
  727. offset = pmac_get_partition(part);
  728. if (offset < 0)
  729. return offset;
  730. if (copy_to_user((void __user*)arg, &offset, sizeof(offset)) != 0)
  731. return -EFAULT;
  732. return 0;
  733. }
  734. #endif /* CONFIG_PPC_PMAC */
  735. default:
  736. return -EINVAL;
  737. }
  738. }
  739. static const struct file_operations nvram_fops = {
  740. .owner = THIS_MODULE,
  741. .llseek = dev_nvram_llseek,
  742. .read = dev_nvram_read,
  743. .write = dev_nvram_write,
  744. .unlocked_ioctl = dev_nvram_ioctl,
  745. };
  746. static struct miscdevice nvram_dev = {
  747. NVRAM_MINOR,
  748. "nvram",
  749. &nvram_fops
  750. };
  751. #ifdef DEBUG_NVRAM
  752. static void __init nvram_print_partitions(char * label)
  753. {
  754. struct nvram_partition * tmp_part;
  755. printk(KERN_WARNING "--------%s---------\n", label);
  756. printk(KERN_WARNING "indx\t\tsig\tchks\tlen\tname\n");
  757. list_for_each_entry(tmp_part, &nvram_partitions, partition) {
  758. printk(KERN_WARNING "%4d \t%02x\t%02x\t%d\t%12.12s\n",
  759. tmp_part->index, tmp_part->header.signature,
  760. tmp_part->header.checksum, tmp_part->header.length,
  761. tmp_part->header.name);
  762. }
  763. }
  764. #endif
  765. static int __init nvram_write_header(struct nvram_partition * part)
  766. {
  767. loff_t tmp_index;
  768. int rc;
  769. struct nvram_header phead;
  770. memcpy(&phead, &part->header, NVRAM_HEADER_LEN);
  771. phead.length = cpu_to_be16(phead.length);
  772. tmp_index = part->index;
  773. rc = ppc_md.nvram_write((char *)&phead, NVRAM_HEADER_LEN, &tmp_index);
  774. return rc;
  775. }
  776. static unsigned char __init nvram_checksum(struct nvram_header *p)
  777. {
  778. unsigned int c_sum, c_sum2;
  779. unsigned short *sp = (unsigned short *)p->name; /* assume 6 shorts */
  780. c_sum = p->signature + p->length + sp[0] + sp[1] + sp[2] + sp[3] + sp[4] + sp[5];
  781. /* The sum may have spilled into the 3rd byte. Fold it back. */
  782. c_sum = ((c_sum & 0xffff) + (c_sum >> 16)) & 0xffff;
  783. /* The sum cannot exceed 2 bytes. Fold it into a checksum */
  784. c_sum2 = (c_sum >> 8) + (c_sum << 8);
  785. c_sum = ((c_sum + c_sum2) >> 8) & 0xff;
  786. return c_sum;
  787. }
  788. /*
  789. * Per the criteria passed via nvram_remove_partition(), should this
  790. * partition be removed? 1=remove, 0=keep
  791. */
  792. static int nvram_can_remove_partition(struct nvram_partition *part,
  793. const char *name, int sig, const char *exceptions[])
  794. {
  795. if (part->header.signature != sig)
  796. return 0;
  797. if (name) {
  798. if (strncmp(name, part->header.name, 12))
  799. return 0;
  800. } else if (exceptions) {
  801. const char **except;
  802. for (except = exceptions; *except; except++) {
  803. if (!strncmp(*except, part->header.name, 12))
  804. return 0;
  805. }
  806. }
  807. return 1;
  808. }
  809. /**
  810. * nvram_remove_partition - Remove one or more partitions in nvram
  811. * @name: name of the partition to remove, or NULL for a
  812. * signature only match
  813. * @sig: signature of the partition(s) to remove
  814. * @exceptions: When removing all partitions with a matching signature,
  815. * leave these alone.
  816. */
  817. int __init nvram_remove_partition(const char *name, int sig,
  818. const char *exceptions[])
  819. {
  820. struct nvram_partition *part, *prev, *tmp;
  821. int rc;
  822. list_for_each_entry(part, &nvram_partitions, partition) {
  823. if (!nvram_can_remove_partition(part, name, sig, exceptions))
  824. continue;
  825. /* Make partition a free partition */
  826. part->header.signature = NVRAM_SIG_FREE;
  827. memset(part->header.name, 'w', 12);
  828. part->header.checksum = nvram_checksum(&part->header);
  829. rc = nvram_write_header(part);
  830. if (rc <= 0) {
  831. printk(KERN_ERR "nvram_remove_partition: nvram_write failed (%d)\n", rc);
  832. return rc;
  833. }
  834. }
  835. /* Merge contiguous ones */
  836. prev = NULL;
  837. list_for_each_entry_safe(part, tmp, &nvram_partitions, partition) {
  838. if (part->header.signature != NVRAM_SIG_FREE) {
  839. prev = NULL;
  840. continue;
  841. }
  842. if (prev) {
  843. prev->header.length += part->header.length;
  844. prev->header.checksum = nvram_checksum(&prev->header);
  845. rc = nvram_write_header(prev);
  846. if (rc <= 0) {
  847. printk(KERN_ERR "nvram_remove_partition: nvram_write failed (%d)\n", rc);
  848. return rc;
  849. }
  850. list_del(&part->partition);
  851. kfree(part);
  852. } else
  853. prev = part;
  854. }
  855. return 0;
  856. }
  857. /**
  858. * nvram_create_partition - Create a partition in nvram
  859. * @name: name of the partition to create
  860. * @sig: signature of the partition to create
  861. * @req_size: size of data to allocate in bytes
  862. * @min_size: minimum acceptable size (0 means req_size)
  863. *
  864. * Returns a negative error code or a positive nvram index
  865. * of the beginning of the data area of the newly created
  866. * partition. If you provided a min_size smaller than req_size
  867. * you need to query for the actual size yourself after the
  868. * call using nvram_partition_get_size().
  869. */
  870. loff_t __init nvram_create_partition(const char *name, int sig,
  871. int req_size, int min_size)
  872. {
  873. struct nvram_partition *part;
  874. struct nvram_partition *new_part;
  875. struct nvram_partition *free_part = NULL;
  876. static char nv_init_vals[16];
  877. loff_t tmp_index;
  878. long size = 0;
  879. int rc;
  880. /* Convert sizes from bytes to blocks */
  881. req_size = _ALIGN_UP(req_size, NVRAM_BLOCK_LEN) / NVRAM_BLOCK_LEN;
  882. min_size = _ALIGN_UP(min_size, NVRAM_BLOCK_LEN) / NVRAM_BLOCK_LEN;
  883. /* If no minimum size specified, make it the same as the
  884. * requested size
  885. */
  886. if (min_size == 0)
  887. min_size = req_size;
  888. if (min_size > req_size)
  889. return -EINVAL;
  890. /* Now add one block to each for the header */
  891. req_size += 1;
  892. min_size += 1;
  893. /* Find a free partition that will give us the maximum needed size
  894. If can't find one that will give us the minimum size needed */
  895. list_for_each_entry(part, &nvram_partitions, partition) {
  896. if (part->header.signature != NVRAM_SIG_FREE)
  897. continue;
  898. if (part->header.length >= req_size) {
  899. size = req_size;
  900. free_part = part;
  901. break;
  902. }
  903. if (part->header.length > size &&
  904. part->header.length >= min_size) {
  905. size = part->header.length;
  906. free_part = part;
  907. }
  908. }
  909. if (!size)
  910. return -ENOSPC;
  911. /* Create our OS partition */
  912. new_part = kmalloc(sizeof(*new_part), GFP_KERNEL);
  913. if (!new_part) {
  914. pr_err("%s: kmalloc failed\n", __func__);
  915. return -ENOMEM;
  916. }
  917. new_part->index = free_part->index;
  918. new_part->header.signature = sig;
  919. new_part->header.length = size;
  920. strncpy(new_part->header.name, name, 12);
  921. new_part->header.checksum = nvram_checksum(&new_part->header);
  922. rc = nvram_write_header(new_part);
  923. if (rc <= 0) {
  924. pr_err("%s: nvram_write_header failed (%d)\n", __func__, rc);
  925. kfree(new_part);
  926. return rc;
  927. }
  928. list_add_tail(&new_part->partition, &free_part->partition);
  929. /* Adjust or remove the partition we stole the space from */
  930. if (free_part->header.length > size) {
  931. free_part->index += size * NVRAM_BLOCK_LEN;
  932. free_part->header.length -= size;
  933. free_part->header.checksum = nvram_checksum(&free_part->header);
  934. rc = nvram_write_header(free_part);
  935. if (rc <= 0) {
  936. pr_err("%s: nvram_write_header failed (%d)\n",
  937. __func__, rc);
  938. return rc;
  939. }
  940. } else {
  941. list_del(&free_part->partition);
  942. kfree(free_part);
  943. }
  944. /* Clear the new partition */
  945. for (tmp_index = new_part->index + NVRAM_HEADER_LEN;
  946. tmp_index < ((size - 1) * NVRAM_BLOCK_LEN);
  947. tmp_index += NVRAM_BLOCK_LEN) {
  948. rc = ppc_md.nvram_write(nv_init_vals, NVRAM_BLOCK_LEN, &tmp_index);
  949. if (rc <= 0) {
  950. pr_err("%s: nvram_write failed (%d)\n",
  951. __func__, rc);
  952. return rc;
  953. }
  954. }
  955. return new_part->index + NVRAM_HEADER_LEN;
  956. }
  957. /**
  958. * nvram_get_partition_size - Get the data size of an nvram partition
  959. * @data_index: This is the offset of the start of the data of
  960. * the partition. The same value that is returned by
  961. * nvram_create_partition().
  962. */
  963. int nvram_get_partition_size(loff_t data_index)
  964. {
  965. struct nvram_partition *part;
  966. list_for_each_entry(part, &nvram_partitions, partition) {
  967. if (part->index + NVRAM_HEADER_LEN == data_index)
  968. return (part->header.length - 1) * NVRAM_BLOCK_LEN;
  969. }
  970. return -1;
  971. }
  972. /**
  973. * nvram_find_partition - Find an nvram partition by signature and name
  974. * @name: Name of the partition or NULL for any name
  975. * @sig: Signature to test against
  976. * @out_size: if non-NULL, returns the size of the data part of the partition
  977. */
  978. loff_t nvram_find_partition(const char *name, int sig, int *out_size)
  979. {
  980. struct nvram_partition *p;
  981. list_for_each_entry(p, &nvram_partitions, partition) {
  982. if (p->header.signature == sig &&
  983. (!name || !strncmp(p->header.name, name, 12))) {
  984. if (out_size)
  985. *out_size = (p->header.length - 1) *
  986. NVRAM_BLOCK_LEN;
  987. return p->index + NVRAM_HEADER_LEN;
  988. }
  989. }
  990. return 0;
  991. }
  992. int __init nvram_scan_partitions(void)
  993. {
  994. loff_t cur_index = 0;
  995. struct nvram_header phead;
  996. struct nvram_partition * tmp_part;
  997. unsigned char c_sum;
  998. char * header;
  999. int total_size;
  1000. int err;
  1001. if (ppc_md.nvram_size == NULL || ppc_md.nvram_size() <= 0)
  1002. return -ENODEV;
  1003. total_size = ppc_md.nvram_size();
  1004. header = kmalloc(NVRAM_HEADER_LEN, GFP_KERNEL);
  1005. if (!header) {
  1006. printk(KERN_ERR "nvram_scan_partitions: Failed kmalloc\n");
  1007. return -ENOMEM;
  1008. }
  1009. while (cur_index < total_size) {
  1010. err = ppc_md.nvram_read(header, NVRAM_HEADER_LEN, &cur_index);
  1011. if (err != NVRAM_HEADER_LEN) {
  1012. printk(KERN_ERR "nvram_scan_partitions: Error parsing "
  1013. "nvram partitions\n");
  1014. goto out;
  1015. }
  1016. cur_index -= NVRAM_HEADER_LEN; /* nvram_read will advance us */
  1017. memcpy(&phead, header, NVRAM_HEADER_LEN);
  1018. phead.length = be16_to_cpu(phead.length);
  1019. err = 0;
  1020. c_sum = nvram_checksum(&phead);
  1021. if (c_sum != phead.checksum) {
  1022. printk(KERN_WARNING "WARNING: nvram partition checksum"
  1023. " was %02x, should be %02x!\n",
  1024. phead.checksum, c_sum);
  1025. printk(KERN_WARNING "Terminating nvram partition scan\n");
  1026. goto out;
  1027. }
  1028. if (!phead.length) {
  1029. printk(KERN_WARNING "WARNING: nvram corruption "
  1030. "detected: 0-length partition\n");
  1031. goto out;
  1032. }
  1033. tmp_part = kmalloc(sizeof(struct nvram_partition), GFP_KERNEL);
  1034. err = -ENOMEM;
  1035. if (!tmp_part) {
  1036. printk(KERN_ERR "nvram_scan_partitions: kmalloc failed\n");
  1037. goto out;
  1038. }
  1039. memcpy(&tmp_part->header, &phead, NVRAM_HEADER_LEN);
  1040. tmp_part->index = cur_index;
  1041. list_add_tail(&tmp_part->partition, &nvram_partitions);
  1042. cur_index += phead.length * NVRAM_BLOCK_LEN;
  1043. }
  1044. err = 0;
  1045. #ifdef DEBUG_NVRAM
  1046. nvram_print_partitions("NVRAM Partitions");
  1047. #endif
  1048. out:
  1049. kfree(header);
  1050. return err;
  1051. }
  1052. static int __init nvram_init(void)
  1053. {
  1054. int rc;
  1055. BUILD_BUG_ON(NVRAM_BLOCK_LEN != 16);
  1056. if (ppc_md.nvram_size == NULL || ppc_md.nvram_size() <= 0)
  1057. return -ENODEV;
  1058. rc = misc_register(&nvram_dev);
  1059. if (rc != 0) {
  1060. printk(KERN_ERR "nvram_init: failed to register device\n");
  1061. return rc;
  1062. }
  1063. return rc;
  1064. }
  1065. device_initcall(nvram_init);