nvram_64.c 31 KB

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