nvram_64.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212
  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(info), &tmp_index);
  187. if (rc <= 0) {
  188. pr_err("%s: Failed nvram_write (%d)\n", __func__, rc);
  189. return rc;
  190. }
  191. rc = ppc_md.nvram_write(buff, length, &tmp_index);
  192. if (rc <= 0) {
  193. pr_err("%s: Failed nvram_write (%d)\n", __func__, rc);
  194. return rc;
  195. }
  196. return 0;
  197. }
  198. /* nvram_read_partition
  199. *
  200. * Reads nvram partition for at most 'length'
  201. */
  202. int nvram_read_partition(struct nvram_os_partition *part, char *buff,
  203. int length, unsigned int *err_type,
  204. unsigned int *error_log_cnt)
  205. {
  206. int rc;
  207. loff_t tmp_index;
  208. struct err_log_info info;
  209. if (part->index == -1)
  210. return -1;
  211. if (length > part->size)
  212. length = part->size;
  213. tmp_index = part->index;
  214. if (part->os_partition) {
  215. rc = ppc_md.nvram_read((char *)&info, sizeof(info), &tmp_index);
  216. if (rc <= 0) {
  217. pr_err("%s: Failed nvram_read (%d)\n", __func__, rc);
  218. return rc;
  219. }
  220. }
  221. rc = ppc_md.nvram_read(buff, length, &tmp_index);
  222. if (rc <= 0) {
  223. pr_err("%s: Failed nvram_read (%d)\n", __func__, rc);
  224. return rc;
  225. }
  226. if (part->os_partition) {
  227. *error_log_cnt = be32_to_cpu(info.seq_num);
  228. *err_type = be32_to_cpu(info.error_type);
  229. }
  230. return 0;
  231. }
  232. /* nvram_init_os_partition
  233. *
  234. * This sets up a partition with an "OS" signature.
  235. *
  236. * The general strategy is the following:
  237. * 1.) If a partition with the indicated name already exists...
  238. * - If it's large enough, use it.
  239. * - Otherwise, recycle it and keep going.
  240. * 2.) Search for a free partition that is large enough.
  241. * 3.) If there's not a free partition large enough, recycle any obsolete
  242. * OS partitions and try again.
  243. * 4.) Will first try getting a chunk that will satisfy the requested size.
  244. * 5.) If a chunk of the requested size cannot be allocated, then try finding
  245. * a chunk that will satisfy the minum needed.
  246. *
  247. * Returns 0 on success, else -1.
  248. */
  249. int __init nvram_init_os_partition(struct nvram_os_partition *part)
  250. {
  251. loff_t p;
  252. int size;
  253. /* Look for ours */
  254. p = nvram_find_partition(part->name, NVRAM_SIG_OS, &size);
  255. /* Found one but too small, remove it */
  256. if (p && size < part->min_size) {
  257. pr_info("nvram: Found too small %s partition,"
  258. " removing it...\n", part->name);
  259. nvram_remove_partition(part->name, NVRAM_SIG_OS, NULL);
  260. p = 0;
  261. }
  262. /* Create one if we didn't find */
  263. if (!p) {
  264. p = nvram_create_partition(part->name, NVRAM_SIG_OS,
  265. part->req_size, part->min_size);
  266. if (p == -ENOSPC) {
  267. pr_info("nvram: No room to create %s partition, "
  268. "deleting any obsolete OS partitions...\n",
  269. part->name);
  270. nvram_remove_partition(NULL, NVRAM_SIG_OS,
  271. nvram_os_partitions);
  272. p = nvram_create_partition(part->name, NVRAM_SIG_OS,
  273. part->req_size, part->min_size);
  274. }
  275. }
  276. if (p <= 0) {
  277. pr_err("nvram: Failed to find or create %s"
  278. " partition, err %d\n", part->name, (int)p);
  279. return -1;
  280. }
  281. part->index = p;
  282. part->size = nvram_get_partition_size(p) - sizeof(struct err_log_info);
  283. return 0;
  284. }
  285. /* Derived from logfs_compress() */
  286. static int nvram_compress(const void *in, void *out, size_t inlen,
  287. size_t outlen)
  288. {
  289. int err, ret;
  290. ret = -EIO;
  291. err = zlib_deflateInit2(&stream, COMPR_LEVEL, Z_DEFLATED, WINDOW_BITS,
  292. MEM_LEVEL, Z_DEFAULT_STRATEGY);
  293. if (err != Z_OK)
  294. goto error;
  295. stream.next_in = in;
  296. stream.avail_in = inlen;
  297. stream.total_in = 0;
  298. stream.next_out = out;
  299. stream.avail_out = outlen;
  300. stream.total_out = 0;
  301. err = zlib_deflate(&stream, Z_FINISH);
  302. if (err != Z_STREAM_END)
  303. goto error;
  304. err = zlib_deflateEnd(&stream);
  305. if (err != Z_OK)
  306. goto error;
  307. if (stream.total_out >= stream.total_in)
  308. goto error;
  309. ret = stream.total_out;
  310. error:
  311. return ret;
  312. }
  313. /* Compress the text from big_oops_buf into oops_buf. */
  314. static int zip_oops(size_t text_len)
  315. {
  316. struct oops_log_info *oops_hdr = (struct oops_log_info *)oops_buf;
  317. int zipped_len = nvram_compress(big_oops_buf, oops_data, text_len,
  318. oops_data_sz);
  319. if (zipped_len < 0) {
  320. pr_err("nvram: compression failed; returned %d\n", zipped_len);
  321. pr_err("nvram: logging uncompressed oops/panic report\n");
  322. return -1;
  323. }
  324. oops_hdr->version = cpu_to_be16(OOPS_HDR_VERSION);
  325. oops_hdr->report_length = cpu_to_be16(zipped_len);
  326. oops_hdr->timestamp = cpu_to_be64(ktime_get_real_seconds());
  327. return 0;
  328. }
  329. #ifdef CONFIG_PSTORE
  330. static int nvram_pstore_open(struct pstore_info *psi)
  331. {
  332. /* Reset the iterator to start reading partitions again */
  333. read_type = -1;
  334. return 0;
  335. }
  336. /**
  337. * nvram_pstore_write - pstore write callback for nvram
  338. * @record: pstore record to write, with @id to be set
  339. *
  340. * Called by pstore_dump() when an oops or panic report is logged in the
  341. * printk buffer.
  342. * Returns 0 on successful write.
  343. */
  344. static int nvram_pstore_write(struct pstore_record *record)
  345. {
  346. int rc;
  347. unsigned int err_type = ERR_TYPE_KERNEL_PANIC;
  348. struct oops_log_info *oops_hdr = (struct oops_log_info *) oops_buf;
  349. /* part 1 has the recent messages from printk buffer */
  350. if (record->part > 1 || (record->type != PSTORE_TYPE_DMESG))
  351. return -1;
  352. if (clobbering_unread_rtas_event())
  353. return -1;
  354. oops_hdr->version = cpu_to_be16(OOPS_HDR_VERSION);
  355. oops_hdr->report_length = cpu_to_be16(record->size);
  356. oops_hdr->timestamp = cpu_to_be64(ktime_get_real_seconds());
  357. if (record->compressed)
  358. err_type = ERR_TYPE_KERNEL_PANIC_GZ;
  359. rc = nvram_write_os_partition(&oops_log_partition, oops_buf,
  360. (int) (sizeof(*oops_hdr) + record->size), err_type,
  361. record->count);
  362. if (rc != 0)
  363. return rc;
  364. record->id = record->part;
  365. return 0;
  366. }
  367. /*
  368. * Reads the oops/panic report, rtas, of-config and common partition.
  369. * Returns the length of the data we read from each partition.
  370. * Returns 0 if we've been called before.
  371. */
  372. static ssize_t nvram_pstore_read(struct pstore_record *record)
  373. {
  374. struct oops_log_info *oops_hdr;
  375. unsigned int err_type, id_no, size = 0;
  376. struct nvram_os_partition *part = NULL;
  377. char *buff = NULL;
  378. int sig = 0;
  379. loff_t p;
  380. read_type++;
  381. switch (nvram_type_ids[read_type]) {
  382. case PSTORE_TYPE_DMESG:
  383. part = &oops_log_partition;
  384. record->type = PSTORE_TYPE_DMESG;
  385. break;
  386. case PSTORE_TYPE_PPC_COMMON:
  387. sig = NVRAM_SIG_SYS;
  388. part = &common_partition;
  389. record->type = PSTORE_TYPE_PPC_COMMON;
  390. record->id = PSTORE_TYPE_PPC_COMMON;
  391. record->time.tv_sec = 0;
  392. record->time.tv_nsec = 0;
  393. break;
  394. #ifdef CONFIG_PPC_PSERIES
  395. case PSTORE_TYPE_PPC_RTAS:
  396. part = &rtas_log_partition;
  397. record->type = PSTORE_TYPE_PPC_RTAS;
  398. record->time.tv_sec = last_rtas_event;
  399. record->time.tv_nsec = 0;
  400. break;
  401. case PSTORE_TYPE_PPC_OF:
  402. sig = NVRAM_SIG_OF;
  403. part = &of_config_partition;
  404. record->type = PSTORE_TYPE_PPC_OF;
  405. record->id = PSTORE_TYPE_PPC_OF;
  406. record->time.tv_sec = 0;
  407. record->time.tv_nsec = 0;
  408. break;
  409. #endif
  410. #ifdef CONFIG_PPC_POWERNV
  411. case PSTORE_TYPE_PPC_OPAL:
  412. sig = NVRAM_SIG_FW;
  413. part = &skiboot_partition;
  414. record->type = PSTORE_TYPE_PPC_OPAL;
  415. record->id = PSTORE_TYPE_PPC_OPAL;
  416. record->time.tv_sec = 0;
  417. record->time.tv_nsec = 0;
  418. break;
  419. #endif
  420. default:
  421. return 0;
  422. }
  423. if (!part->os_partition) {
  424. p = nvram_find_partition(part->name, sig, &size);
  425. if (p <= 0) {
  426. pr_err("nvram: Failed to find partition %s, "
  427. "err %d\n", part->name, (int)p);
  428. return 0;
  429. }
  430. part->index = p;
  431. part->size = size;
  432. }
  433. buff = kmalloc(part->size, GFP_KERNEL);
  434. if (!buff)
  435. return -ENOMEM;
  436. if (nvram_read_partition(part, buff, part->size, &err_type, &id_no)) {
  437. kfree(buff);
  438. return 0;
  439. }
  440. record->count = 0;
  441. if (part->os_partition)
  442. record->id = id_no;
  443. if (nvram_type_ids[read_type] == PSTORE_TYPE_DMESG) {
  444. size_t length, hdr_size;
  445. oops_hdr = (struct oops_log_info *)buff;
  446. if (be16_to_cpu(oops_hdr->version) < OOPS_HDR_VERSION) {
  447. /* Old format oops header had 2-byte record size */
  448. hdr_size = sizeof(u16);
  449. length = be16_to_cpu(oops_hdr->version);
  450. record->time.tv_sec = 0;
  451. record->time.tv_nsec = 0;
  452. } else {
  453. hdr_size = sizeof(*oops_hdr);
  454. length = be16_to_cpu(oops_hdr->report_length);
  455. record->time.tv_sec = be64_to_cpu(oops_hdr->timestamp);
  456. record->time.tv_nsec = 0;
  457. }
  458. record->buf = kmemdup(buff + hdr_size, length, GFP_KERNEL);
  459. kfree(buff);
  460. if (record->buf == NULL)
  461. return -ENOMEM;
  462. record->ecc_notice_size = 0;
  463. if (err_type == ERR_TYPE_KERNEL_PANIC_GZ)
  464. record->compressed = true;
  465. else
  466. record->compressed = false;
  467. return length;
  468. }
  469. record->buf = buff;
  470. return part->size;
  471. }
  472. static struct pstore_info nvram_pstore_info = {
  473. .owner = THIS_MODULE,
  474. .name = "nvram",
  475. .flags = PSTORE_FLAGS_DMESG,
  476. .open = nvram_pstore_open,
  477. .read = nvram_pstore_read,
  478. .write = nvram_pstore_write,
  479. };
  480. static int nvram_pstore_init(void)
  481. {
  482. int rc = 0;
  483. if (machine_is(pseries)) {
  484. nvram_type_ids[2] = PSTORE_TYPE_PPC_RTAS;
  485. nvram_type_ids[3] = PSTORE_TYPE_PPC_OF;
  486. } else
  487. nvram_type_ids[2] = PSTORE_TYPE_PPC_OPAL;
  488. nvram_pstore_info.buf = oops_data;
  489. nvram_pstore_info.bufsize = oops_data_sz;
  490. rc = pstore_register(&nvram_pstore_info);
  491. if (rc && (rc != -EPERM))
  492. /* Print error only when pstore.backend == nvram */
  493. pr_err("nvram: pstore_register() failed, returned %d. "
  494. "Defaults to kmsg_dump\n", rc);
  495. return rc;
  496. }
  497. #else
  498. static int nvram_pstore_init(void)
  499. {
  500. return -1;
  501. }
  502. #endif
  503. void __init nvram_init_oops_partition(int rtas_partition_exists)
  504. {
  505. int rc;
  506. rc = nvram_init_os_partition(&oops_log_partition);
  507. if (rc != 0) {
  508. #ifdef CONFIG_PPC_PSERIES
  509. if (!rtas_partition_exists) {
  510. pr_err("nvram: Failed to initialize oops partition!");
  511. return;
  512. }
  513. pr_notice("nvram: Using %s partition to log both"
  514. " RTAS errors and oops/panic reports\n",
  515. rtas_log_partition.name);
  516. memcpy(&oops_log_partition, &rtas_log_partition,
  517. sizeof(rtas_log_partition));
  518. #else
  519. pr_err("nvram: Failed to initialize oops partition!");
  520. return;
  521. #endif
  522. }
  523. oops_buf = kmalloc(oops_log_partition.size, GFP_KERNEL);
  524. if (!oops_buf) {
  525. pr_err("nvram: No memory for %s partition\n",
  526. oops_log_partition.name);
  527. return;
  528. }
  529. oops_data = oops_buf + sizeof(struct oops_log_info);
  530. oops_data_sz = oops_log_partition.size - sizeof(struct oops_log_info);
  531. rc = nvram_pstore_init();
  532. if (!rc)
  533. return;
  534. /*
  535. * Figure compression (preceded by elimination of each line's <n>
  536. * severity prefix) will reduce the oops/panic report to at most
  537. * 45% of its original size.
  538. */
  539. big_oops_buf_sz = (oops_data_sz * 100) / 45;
  540. big_oops_buf = kmalloc(big_oops_buf_sz, GFP_KERNEL);
  541. if (big_oops_buf) {
  542. stream.workspace = kmalloc(zlib_deflate_workspacesize(
  543. WINDOW_BITS, MEM_LEVEL), GFP_KERNEL);
  544. if (!stream.workspace) {
  545. pr_err("nvram: No memory for compression workspace; "
  546. "skipping compression of %s partition data\n",
  547. oops_log_partition.name);
  548. kfree(big_oops_buf);
  549. big_oops_buf = NULL;
  550. }
  551. } else {
  552. pr_err("No memory for uncompressed %s data; "
  553. "skipping compression\n", oops_log_partition.name);
  554. stream.workspace = NULL;
  555. }
  556. rc = kmsg_dump_register(&nvram_kmsg_dumper);
  557. if (rc != 0) {
  558. pr_err("nvram: kmsg_dump_register() failed; returned %d\n", rc);
  559. kfree(oops_buf);
  560. kfree(big_oops_buf);
  561. kfree(stream.workspace);
  562. }
  563. }
  564. /*
  565. * This is our kmsg_dump callback, called after an oops or panic report
  566. * has been written to the printk buffer. We want to capture as much
  567. * of the printk buffer as possible. First, capture as much as we can
  568. * that we think will compress sufficiently to fit in the lnx,oops-log
  569. * partition. If that's too much, go back and capture uncompressed text.
  570. */
  571. static void oops_to_nvram(struct kmsg_dumper *dumper,
  572. enum kmsg_dump_reason reason)
  573. {
  574. struct oops_log_info *oops_hdr = (struct oops_log_info *)oops_buf;
  575. static unsigned int oops_count = 0;
  576. static bool panicking = false;
  577. static DEFINE_SPINLOCK(lock);
  578. unsigned long flags;
  579. size_t text_len;
  580. unsigned int err_type = ERR_TYPE_KERNEL_PANIC_GZ;
  581. int rc = -1;
  582. switch (reason) {
  583. case KMSG_DUMP_RESTART:
  584. case KMSG_DUMP_HALT:
  585. case KMSG_DUMP_POWEROFF:
  586. /* These are almost always orderly shutdowns. */
  587. return;
  588. case KMSG_DUMP_OOPS:
  589. break;
  590. case KMSG_DUMP_PANIC:
  591. panicking = true;
  592. break;
  593. case KMSG_DUMP_EMERG:
  594. if (panicking)
  595. /* Panic report already captured. */
  596. return;
  597. break;
  598. default:
  599. pr_err("%s: ignoring unrecognized KMSG_DUMP_* reason %d\n",
  600. __func__, (int) reason);
  601. return;
  602. }
  603. if (clobbering_unread_rtas_event())
  604. return;
  605. if (!spin_trylock_irqsave(&lock, flags))
  606. return;
  607. if (big_oops_buf) {
  608. kmsg_dump_get_buffer(dumper, false,
  609. big_oops_buf, big_oops_buf_sz, &text_len);
  610. rc = zip_oops(text_len);
  611. }
  612. if (rc != 0) {
  613. kmsg_dump_rewind(dumper);
  614. kmsg_dump_get_buffer(dumper, false,
  615. oops_data, oops_data_sz, &text_len);
  616. err_type = ERR_TYPE_KERNEL_PANIC;
  617. oops_hdr->version = cpu_to_be16(OOPS_HDR_VERSION);
  618. oops_hdr->report_length = cpu_to_be16(text_len);
  619. oops_hdr->timestamp = cpu_to_be64(ktime_get_real_seconds());
  620. }
  621. (void) nvram_write_os_partition(&oops_log_partition, oops_buf,
  622. (int) (sizeof(*oops_hdr) + text_len), err_type,
  623. ++oops_count);
  624. spin_unlock_irqrestore(&lock, flags);
  625. }
  626. static loff_t dev_nvram_llseek(struct file *file, loff_t offset, int origin)
  627. {
  628. if (ppc_md.nvram_size == NULL)
  629. return -ENODEV;
  630. return generic_file_llseek_size(file, offset, origin, MAX_LFS_FILESIZE,
  631. ppc_md.nvram_size());
  632. }
  633. static ssize_t dev_nvram_read(struct file *file, char __user *buf,
  634. size_t count, loff_t *ppos)
  635. {
  636. ssize_t ret;
  637. char *tmp = NULL;
  638. ssize_t size;
  639. if (!ppc_md.nvram_size) {
  640. ret = -ENODEV;
  641. goto out;
  642. }
  643. size = ppc_md.nvram_size();
  644. if (size < 0) {
  645. ret = size;
  646. goto out;
  647. }
  648. if (*ppos >= size) {
  649. ret = 0;
  650. goto out;
  651. }
  652. count = min_t(size_t, count, size - *ppos);
  653. count = min(count, PAGE_SIZE);
  654. tmp = kmalloc(count, GFP_KERNEL);
  655. if (!tmp) {
  656. ret = -ENOMEM;
  657. goto out;
  658. }
  659. ret = ppc_md.nvram_read(tmp, count, ppos);
  660. if (ret <= 0)
  661. goto out;
  662. if (copy_to_user(buf, tmp, ret))
  663. ret = -EFAULT;
  664. out:
  665. kfree(tmp);
  666. return ret;
  667. }
  668. static ssize_t dev_nvram_write(struct file *file, const char __user *buf,
  669. size_t count, loff_t *ppos)
  670. {
  671. ssize_t ret;
  672. char *tmp = NULL;
  673. ssize_t size;
  674. ret = -ENODEV;
  675. if (!ppc_md.nvram_size)
  676. goto out;
  677. ret = 0;
  678. size = ppc_md.nvram_size();
  679. if (*ppos >= size || size < 0)
  680. goto out;
  681. count = min_t(size_t, count, size - *ppos);
  682. count = min(count, PAGE_SIZE);
  683. tmp = memdup_user(buf, count);
  684. if (IS_ERR(tmp)) {
  685. ret = PTR_ERR(tmp);
  686. goto out;
  687. }
  688. ret = ppc_md.nvram_write(tmp, count, ppos);
  689. kfree(tmp);
  690. out:
  691. return ret;
  692. }
  693. static long dev_nvram_ioctl(struct file *file, unsigned int cmd,
  694. unsigned long arg)
  695. {
  696. switch(cmd) {
  697. #ifdef CONFIG_PPC_PMAC
  698. case OBSOLETE_PMAC_NVRAM_GET_OFFSET:
  699. printk(KERN_WARNING "nvram: Using obsolete PMAC_NVRAM_GET_OFFSET ioctl\n");
  700. case IOC_NVRAM_GET_OFFSET: {
  701. int part, offset;
  702. if (!machine_is(powermac))
  703. return -EINVAL;
  704. if (copy_from_user(&part, (void __user*)arg, sizeof(part)) != 0)
  705. return -EFAULT;
  706. if (part < pmac_nvram_OF || part > pmac_nvram_NR)
  707. return -EINVAL;
  708. offset = pmac_get_partition(part);
  709. if (offset < 0)
  710. return offset;
  711. if (copy_to_user((void __user*)arg, &offset, sizeof(offset)) != 0)
  712. return -EFAULT;
  713. return 0;
  714. }
  715. #endif /* CONFIG_PPC_PMAC */
  716. default:
  717. return -EINVAL;
  718. }
  719. }
  720. static const struct file_operations nvram_fops = {
  721. .owner = THIS_MODULE,
  722. .llseek = dev_nvram_llseek,
  723. .read = dev_nvram_read,
  724. .write = dev_nvram_write,
  725. .unlocked_ioctl = dev_nvram_ioctl,
  726. };
  727. static struct miscdevice nvram_dev = {
  728. NVRAM_MINOR,
  729. "nvram",
  730. &nvram_fops
  731. };
  732. #ifdef DEBUG_NVRAM
  733. static void __init nvram_print_partitions(char * label)
  734. {
  735. struct nvram_partition * tmp_part;
  736. printk(KERN_WARNING "--------%s---------\n", label);
  737. printk(KERN_WARNING "indx\t\tsig\tchks\tlen\tname\n");
  738. list_for_each_entry(tmp_part, &nvram_partitions, partition) {
  739. printk(KERN_WARNING "%4d \t%02x\t%02x\t%d\t%12.12s\n",
  740. tmp_part->index, tmp_part->header.signature,
  741. tmp_part->header.checksum, tmp_part->header.length,
  742. tmp_part->header.name);
  743. }
  744. }
  745. #endif
  746. static int __init nvram_write_header(struct nvram_partition * part)
  747. {
  748. loff_t tmp_index;
  749. int rc;
  750. struct nvram_header phead;
  751. memcpy(&phead, &part->header, NVRAM_HEADER_LEN);
  752. phead.length = cpu_to_be16(phead.length);
  753. tmp_index = part->index;
  754. rc = ppc_md.nvram_write((char *)&phead, NVRAM_HEADER_LEN, &tmp_index);
  755. return rc;
  756. }
  757. static unsigned char __init nvram_checksum(struct nvram_header *p)
  758. {
  759. unsigned int c_sum, c_sum2;
  760. unsigned short *sp = (unsigned short *)p->name; /* assume 6 shorts */
  761. c_sum = p->signature + p->length + sp[0] + sp[1] + sp[2] + sp[3] + sp[4] + sp[5];
  762. /* The sum may have spilled into the 3rd byte. Fold it back. */
  763. c_sum = ((c_sum & 0xffff) + (c_sum >> 16)) & 0xffff;
  764. /* The sum cannot exceed 2 bytes. Fold it into a checksum */
  765. c_sum2 = (c_sum >> 8) + (c_sum << 8);
  766. c_sum = ((c_sum + c_sum2) >> 8) & 0xff;
  767. return c_sum;
  768. }
  769. /*
  770. * Per the criteria passed via nvram_remove_partition(), should this
  771. * partition be removed? 1=remove, 0=keep
  772. */
  773. static int nvram_can_remove_partition(struct nvram_partition *part,
  774. const char *name, int sig, const char *exceptions[])
  775. {
  776. if (part->header.signature != sig)
  777. return 0;
  778. if (name) {
  779. if (strncmp(name, part->header.name, 12))
  780. return 0;
  781. } else if (exceptions) {
  782. const char **except;
  783. for (except = exceptions; *except; except++) {
  784. if (!strncmp(*except, part->header.name, 12))
  785. return 0;
  786. }
  787. }
  788. return 1;
  789. }
  790. /**
  791. * nvram_remove_partition - Remove one or more partitions in nvram
  792. * @name: name of the partition to remove, or NULL for a
  793. * signature only match
  794. * @sig: signature of the partition(s) to remove
  795. * @exceptions: When removing all partitions with a matching signature,
  796. * leave these alone.
  797. */
  798. int __init nvram_remove_partition(const char *name, int sig,
  799. const char *exceptions[])
  800. {
  801. struct nvram_partition *part, *prev, *tmp;
  802. int rc;
  803. list_for_each_entry(part, &nvram_partitions, partition) {
  804. if (!nvram_can_remove_partition(part, name, sig, exceptions))
  805. continue;
  806. /* Make partition a free partition */
  807. part->header.signature = NVRAM_SIG_FREE;
  808. memset(part->header.name, 'w', 12);
  809. part->header.checksum = nvram_checksum(&part->header);
  810. rc = nvram_write_header(part);
  811. if (rc <= 0) {
  812. printk(KERN_ERR "nvram_remove_partition: nvram_write failed (%d)\n", rc);
  813. return rc;
  814. }
  815. }
  816. /* Merge contiguous ones */
  817. prev = NULL;
  818. list_for_each_entry_safe(part, tmp, &nvram_partitions, partition) {
  819. if (part->header.signature != NVRAM_SIG_FREE) {
  820. prev = NULL;
  821. continue;
  822. }
  823. if (prev) {
  824. prev->header.length += part->header.length;
  825. prev->header.checksum = nvram_checksum(&prev->header);
  826. rc = nvram_write_header(prev);
  827. if (rc <= 0) {
  828. printk(KERN_ERR "nvram_remove_partition: nvram_write failed (%d)\n", rc);
  829. return rc;
  830. }
  831. list_del(&part->partition);
  832. kfree(part);
  833. } else
  834. prev = part;
  835. }
  836. return 0;
  837. }
  838. /**
  839. * nvram_create_partition - Create a partition in nvram
  840. * @name: name of the partition to create
  841. * @sig: signature of the partition to create
  842. * @req_size: size of data to allocate in bytes
  843. * @min_size: minimum acceptable size (0 means req_size)
  844. *
  845. * Returns a negative error code or a positive nvram index
  846. * of the beginning of the data area of the newly created
  847. * partition. If you provided a min_size smaller than req_size
  848. * you need to query for the actual size yourself after the
  849. * call using nvram_partition_get_size().
  850. */
  851. loff_t __init nvram_create_partition(const char *name, int sig,
  852. int req_size, int min_size)
  853. {
  854. struct nvram_partition *part;
  855. struct nvram_partition *new_part;
  856. struct nvram_partition *free_part = NULL;
  857. static char nv_init_vals[16];
  858. loff_t tmp_index;
  859. long size = 0;
  860. int rc;
  861. /* Convert sizes from bytes to blocks */
  862. req_size = _ALIGN_UP(req_size, NVRAM_BLOCK_LEN) / NVRAM_BLOCK_LEN;
  863. min_size = _ALIGN_UP(min_size, NVRAM_BLOCK_LEN) / NVRAM_BLOCK_LEN;
  864. /* If no minimum size specified, make it the same as the
  865. * requested size
  866. */
  867. if (min_size == 0)
  868. min_size = req_size;
  869. if (min_size > req_size)
  870. return -EINVAL;
  871. /* Now add one block to each for the header */
  872. req_size += 1;
  873. min_size += 1;
  874. /* Find a free partition that will give us the maximum needed size
  875. If can't find one that will give us the minimum size needed */
  876. list_for_each_entry(part, &nvram_partitions, partition) {
  877. if (part->header.signature != NVRAM_SIG_FREE)
  878. continue;
  879. if (part->header.length >= req_size) {
  880. size = req_size;
  881. free_part = part;
  882. break;
  883. }
  884. if (part->header.length > size &&
  885. part->header.length >= min_size) {
  886. size = part->header.length;
  887. free_part = part;
  888. }
  889. }
  890. if (!size)
  891. return -ENOSPC;
  892. /* Create our OS partition */
  893. new_part = kzalloc(sizeof(*new_part), GFP_KERNEL);
  894. if (!new_part) {
  895. pr_err("%s: kmalloc failed\n", __func__);
  896. return -ENOMEM;
  897. }
  898. new_part->index = free_part->index;
  899. new_part->header.signature = sig;
  900. new_part->header.length = size;
  901. memcpy(new_part->header.name, name, strnlen(name, sizeof(new_part->header.name)));
  902. new_part->header.checksum = nvram_checksum(&new_part->header);
  903. rc = nvram_write_header(new_part);
  904. if (rc <= 0) {
  905. pr_err("%s: nvram_write_header failed (%d)\n", __func__, rc);
  906. kfree(new_part);
  907. return rc;
  908. }
  909. list_add_tail(&new_part->partition, &free_part->partition);
  910. /* Adjust or remove the partition we stole the space from */
  911. if (free_part->header.length > size) {
  912. free_part->index += size * NVRAM_BLOCK_LEN;
  913. free_part->header.length -= size;
  914. free_part->header.checksum = nvram_checksum(&free_part->header);
  915. rc = nvram_write_header(free_part);
  916. if (rc <= 0) {
  917. pr_err("%s: nvram_write_header failed (%d)\n",
  918. __func__, rc);
  919. return rc;
  920. }
  921. } else {
  922. list_del(&free_part->partition);
  923. kfree(free_part);
  924. }
  925. /* Clear the new partition */
  926. for (tmp_index = new_part->index + NVRAM_HEADER_LEN;
  927. tmp_index < ((size - 1) * NVRAM_BLOCK_LEN);
  928. tmp_index += NVRAM_BLOCK_LEN) {
  929. rc = ppc_md.nvram_write(nv_init_vals, NVRAM_BLOCK_LEN, &tmp_index);
  930. if (rc <= 0) {
  931. pr_err("%s: nvram_write failed (%d)\n",
  932. __func__, rc);
  933. return rc;
  934. }
  935. }
  936. return new_part->index + NVRAM_HEADER_LEN;
  937. }
  938. /**
  939. * nvram_get_partition_size - Get the data size of an nvram partition
  940. * @data_index: This is the offset of the start of the data of
  941. * the partition. The same value that is returned by
  942. * nvram_create_partition().
  943. */
  944. int nvram_get_partition_size(loff_t data_index)
  945. {
  946. struct nvram_partition *part;
  947. list_for_each_entry(part, &nvram_partitions, partition) {
  948. if (part->index + NVRAM_HEADER_LEN == data_index)
  949. return (part->header.length - 1) * NVRAM_BLOCK_LEN;
  950. }
  951. return -1;
  952. }
  953. /**
  954. * nvram_find_partition - Find an nvram partition by signature and name
  955. * @name: Name of the partition or NULL for any name
  956. * @sig: Signature to test against
  957. * @out_size: if non-NULL, returns the size of the data part of the partition
  958. */
  959. loff_t nvram_find_partition(const char *name, int sig, int *out_size)
  960. {
  961. struct nvram_partition *p;
  962. list_for_each_entry(p, &nvram_partitions, partition) {
  963. if (p->header.signature == sig &&
  964. (!name || !strncmp(p->header.name, name, 12))) {
  965. if (out_size)
  966. *out_size = (p->header.length - 1) *
  967. NVRAM_BLOCK_LEN;
  968. return p->index + NVRAM_HEADER_LEN;
  969. }
  970. }
  971. return 0;
  972. }
  973. int __init nvram_scan_partitions(void)
  974. {
  975. loff_t cur_index = 0;
  976. struct nvram_header phead;
  977. struct nvram_partition * tmp_part;
  978. unsigned char c_sum;
  979. char * header;
  980. int total_size;
  981. int err;
  982. if (ppc_md.nvram_size == NULL || ppc_md.nvram_size() <= 0)
  983. return -ENODEV;
  984. total_size = ppc_md.nvram_size();
  985. header = kmalloc(NVRAM_HEADER_LEN, GFP_KERNEL);
  986. if (!header) {
  987. printk(KERN_ERR "nvram_scan_partitions: Failed kmalloc\n");
  988. return -ENOMEM;
  989. }
  990. while (cur_index < total_size) {
  991. err = ppc_md.nvram_read(header, NVRAM_HEADER_LEN, &cur_index);
  992. if (err != NVRAM_HEADER_LEN) {
  993. printk(KERN_ERR "nvram_scan_partitions: Error parsing "
  994. "nvram partitions\n");
  995. goto out;
  996. }
  997. cur_index -= NVRAM_HEADER_LEN; /* nvram_read will advance us */
  998. memcpy(&phead, header, NVRAM_HEADER_LEN);
  999. phead.length = be16_to_cpu(phead.length);
  1000. err = 0;
  1001. c_sum = nvram_checksum(&phead);
  1002. if (c_sum != phead.checksum) {
  1003. printk(KERN_WARNING "WARNING: nvram partition checksum"
  1004. " was %02x, should be %02x!\n",
  1005. phead.checksum, c_sum);
  1006. printk(KERN_WARNING "Terminating nvram partition scan\n");
  1007. goto out;
  1008. }
  1009. if (!phead.length) {
  1010. printk(KERN_WARNING "WARNING: nvram corruption "
  1011. "detected: 0-length partition\n");
  1012. goto out;
  1013. }
  1014. tmp_part = kmalloc(sizeof(*tmp_part), GFP_KERNEL);
  1015. err = -ENOMEM;
  1016. if (!tmp_part) {
  1017. printk(KERN_ERR "nvram_scan_partitions: kmalloc failed\n");
  1018. goto out;
  1019. }
  1020. memcpy(&tmp_part->header, &phead, NVRAM_HEADER_LEN);
  1021. tmp_part->index = cur_index;
  1022. list_add_tail(&tmp_part->partition, &nvram_partitions);
  1023. cur_index += phead.length * NVRAM_BLOCK_LEN;
  1024. }
  1025. err = 0;
  1026. #ifdef DEBUG_NVRAM
  1027. nvram_print_partitions("NVRAM Partitions");
  1028. #endif
  1029. out:
  1030. kfree(header);
  1031. return err;
  1032. }
  1033. static int __init nvram_init(void)
  1034. {
  1035. int rc;
  1036. BUILD_BUG_ON(NVRAM_BLOCK_LEN != 16);
  1037. if (ppc_md.nvram_size == NULL || ppc_md.nvram_size() <= 0)
  1038. return -ENODEV;
  1039. rc = misc_register(&nvram_dev);
  1040. if (rc != 0) {
  1041. printk(KERN_ERR "nvram_init: failed to register device\n");
  1042. return rc;
  1043. }
  1044. return rc;
  1045. }
  1046. device_initcall(nvram_init);