nvram_64.c 27 KB

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