nvram_64.c 31 KB

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