nvram_64.c 31 KB

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