nvram_64.c 31 KB

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