2 * c 2001 PPC 64 Team, IBM Corp
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.
9 * /dev/nvram driver for PPC64
12 #include <linux/types.h>
13 #include <linux/errno.h>
15 #include <linux/miscdevice.h>
16 #include <linux/fcntl.h>
17 #include <linux/nvram.h>
18 #include <linux/init.h>
19 #include <linux/slab.h>
20 #include <linux/spinlock.h>
21 #include <linux/kmsg_dump.h>
22 #include <linux/pagemap.h>
23 #include <linux/pstore.h>
24 #include <linux/zlib.h>
25 #include <linux/uaccess.h>
26 #include <asm/nvram.h>
29 #include <asm/machdep.h>
33 #define NVRAM_HEADER_LEN sizeof(struct nvram_header)
34 #define NVRAM_BLOCK_LEN NVRAM_HEADER_LEN
36 /* If change this size, then change the size of NVNAME_LEN */
38 unsigned char signature;
39 unsigned char checksum;
40 unsigned short length;
41 /* Terminating null required only for names < 12 chars. */
45 struct nvram_partition {
46 struct list_head partition;
47 struct nvram_header header;
51 static LIST_HEAD(nvram_partitions);
53 #ifdef CONFIG_PPC_PSERIES
54 struct nvram_os_partition rtas_log_partition = {
55 .name = "ibm,rtas-log",
63 struct nvram_os_partition oops_log_partition = {
64 .name = "lnx,oops-log",
71 static const char *nvram_os_partitions[] = {
72 #ifdef CONFIG_PPC_PSERIES
79 static void oops_to_nvram(struct kmsg_dumper *dumper,
80 enum kmsg_dump_reason reason);
82 static struct kmsg_dumper nvram_kmsg_dumper = {
87 * For capturing and compressing an oops or panic report...
89 * big_oops_buf[] holds the uncompressed text we're capturing.
91 * oops_buf[] holds the compressed text, preceded by a oops header.
92 * oops header has u16 holding the version of oops header (to differentiate
93 * between old and new format header) followed by u16 holding the length of
94 * the compressed* text (*Or uncompressed, if compression fails.) and u64
95 * holding the timestamp. oops_buf[] gets written to NVRAM.
97 * oops_log_info points to the header. oops_data points to the compressed text.
102 * +-----------+-----------+-----------+------------------------+
103 * | version | length | timestamp | text |
104 * | (2 bytes) | (2 bytes) | (8 bytes) | (oops_data_sz bytes) |
105 * +-----------+-----------+-----------+------------------------+
109 * We preallocate these buffers during init to avoid kmalloc during oops/panic.
111 static size_t big_oops_buf_sz;
112 static char *big_oops_buf, *oops_buf;
113 static char *oops_data;
114 static size_t oops_data_sz;
116 /* Compression parameters */
117 #define COMPR_LEVEL 6
118 #define WINDOW_BITS 12
120 static struct z_stream_s stream;
123 #ifdef CONFIG_PPC_POWERNV
124 static struct nvram_os_partition skiboot_partition = {
125 .name = "ibm,skiboot",
127 .os_partition = false
131 #ifdef CONFIG_PPC_PSERIES
132 static struct nvram_os_partition of_config_partition = {
135 .os_partition = false
139 static struct nvram_os_partition common_partition = {
142 .os_partition = false
145 static enum pstore_type_id nvram_type_ids[] = {
147 PSTORE_TYPE_PPC_COMMON,
152 static int read_type;
155 /* nvram_write_os_partition
157 * We need to buffer the error logs into nvram to ensure that we have
158 * the failure information to decode. If we have a severe error there
159 * is no way to guarantee that the OS or the machine is in a state to
160 * get back to user land and write the error to disk. For example if
161 * the SCSI device driver causes a Machine Check by writing to a bad
162 * IO address, there is no way of guaranteeing that the device driver
163 * is in any state that is would also be able to write the error data
164 * captured to disk, thus we buffer it in NVRAM for analysis on the
167 * In NVRAM the partition containing the error log buffer will looks like:
169 * +-----------+----------+--------+------------+------------------+
170 * | signature | checksum | length | name | data |
171 * |0 |1 |2 3|4 15|16 length-1|
172 * +-----------+----------+--------+------------+------------------+
174 * The 'data' section would look like (in bytes):
175 * +--------------+------------+-----------------------------------+
176 * | event_logged | sequence # | error log |
177 * |0 3|4 7|8 error_log_size-1|
178 * +--------------+------------+-----------------------------------+
180 * event_logged: 0 if event has not been logged to syslog, 1 if it has
181 * sequence #: The unique sequence # for each event. (until it wraps)
182 * error log: The error log from event_scan
184 int nvram_write_os_partition(struct nvram_os_partition *part,
185 char *buff, int length,
186 unsigned int err_type,
187 unsigned int error_log_cnt)
191 struct err_log_info info;
193 if (part->index == -1)
196 if (length > part->size)
199 info.error_type = cpu_to_be32(err_type);
200 info.seq_num = cpu_to_be32(error_log_cnt);
202 tmp_index = part->index;
204 rc = ppc_md.nvram_write((char *)&info, sizeof(info), &tmp_index);
206 pr_err("%s: Failed nvram_write (%d)\n", __func__, rc);
210 rc = ppc_md.nvram_write(buff, length, &tmp_index);
212 pr_err("%s: Failed nvram_write (%d)\n", __func__, rc);
219 /* nvram_read_partition
221 * Reads nvram partition for at most 'length'
223 int nvram_read_partition(struct nvram_os_partition *part, char *buff,
224 int length, unsigned int *err_type,
225 unsigned int *error_log_cnt)
229 struct err_log_info info;
231 if (part->index == -1)
234 if (length > part->size)
237 tmp_index = part->index;
239 if (part->os_partition) {
240 rc = ppc_md.nvram_read((char *)&info, sizeof(info), &tmp_index);
242 pr_err("%s: Failed nvram_read (%d)\n", __func__, rc);
247 rc = ppc_md.nvram_read(buff, length, &tmp_index);
249 pr_err("%s: Failed nvram_read (%d)\n", __func__, rc);
253 if (part->os_partition) {
254 *error_log_cnt = be32_to_cpu(info.seq_num);
255 *err_type = be32_to_cpu(info.error_type);
261 /* nvram_init_os_partition
263 * This sets up a partition with an "OS" signature.
265 * The general strategy is the following:
266 * 1.) If a partition with the indicated name already exists...
267 * - If it's large enough, use it.
268 * - Otherwise, recycle it and keep going.
269 * 2.) Search for a free partition that is large enough.
270 * 3.) If there's not a free partition large enough, recycle any obsolete
271 * OS partitions and try again.
272 * 4.) Will first try getting a chunk that will satisfy the requested size.
273 * 5.) If a chunk of the requested size cannot be allocated, then try finding
274 * a chunk that will satisfy the minum needed.
276 * Returns 0 on success, else -1.
278 int __init nvram_init_os_partition(struct nvram_os_partition *part)
284 p = nvram_find_partition(part->name, NVRAM_SIG_OS, &size);
286 /* Found one but too small, remove it */
287 if (p && size < part->min_size) {
288 pr_info("nvram: Found too small %s partition,"
289 " removing it...\n", part->name);
290 nvram_remove_partition(part->name, NVRAM_SIG_OS, NULL);
294 /* Create one if we didn't find */
296 p = nvram_create_partition(part->name, NVRAM_SIG_OS,
297 part->req_size, part->min_size);
299 pr_info("nvram: No room to create %s partition, "
300 "deleting any obsolete OS partitions...\n",
302 nvram_remove_partition(NULL, NVRAM_SIG_OS,
303 nvram_os_partitions);
304 p = nvram_create_partition(part->name, NVRAM_SIG_OS,
305 part->req_size, part->min_size);
310 pr_err("nvram: Failed to find or create %s"
311 " partition, err %d\n", part->name, (int)p);
316 part->size = nvram_get_partition_size(p) - sizeof(struct err_log_info);
321 /* Derived from logfs_compress() */
322 static int nvram_compress(const void *in, void *out, size_t inlen,
328 err = zlib_deflateInit2(&stream, COMPR_LEVEL, Z_DEFLATED, WINDOW_BITS,
329 MEM_LEVEL, Z_DEFAULT_STRATEGY);
334 stream.avail_in = inlen;
336 stream.next_out = out;
337 stream.avail_out = outlen;
338 stream.total_out = 0;
340 err = zlib_deflate(&stream, Z_FINISH);
341 if (err != Z_STREAM_END)
344 err = zlib_deflateEnd(&stream);
348 if (stream.total_out >= stream.total_in)
351 ret = stream.total_out;
356 /* Compress the text from big_oops_buf into oops_buf. */
357 static int zip_oops(size_t text_len)
359 struct oops_log_info *oops_hdr = (struct oops_log_info *)oops_buf;
360 int zipped_len = nvram_compress(big_oops_buf, oops_data, text_len,
362 if (zipped_len < 0) {
363 pr_err("nvram: compression failed; returned %d\n", zipped_len);
364 pr_err("nvram: logging uncompressed oops/panic report\n");
367 oops_hdr->version = cpu_to_be16(OOPS_HDR_VERSION);
368 oops_hdr->report_length = cpu_to_be16(zipped_len);
369 oops_hdr->timestamp = cpu_to_be64(ktime_get_real_seconds());
374 static int nvram_pstore_open(struct pstore_info *psi)
376 /* Reset the iterator to start reading partitions again */
382 * nvram_pstore_write - pstore write callback for nvram
383 * @record: pstore record to write, with @id to be set
385 * Called by pstore_dump() when an oops or panic report is logged in the
387 * Returns 0 on successful write.
389 static int nvram_pstore_write(struct pstore_record *record)
392 unsigned int err_type = ERR_TYPE_KERNEL_PANIC;
393 struct oops_log_info *oops_hdr = (struct oops_log_info *) oops_buf;
395 /* part 1 has the recent messages from printk buffer */
396 if (record->part > 1 || (record->type != PSTORE_TYPE_DMESG))
399 if (clobbering_unread_rtas_event())
402 oops_hdr->version = cpu_to_be16(OOPS_HDR_VERSION);
403 oops_hdr->report_length = cpu_to_be16(record->size);
404 oops_hdr->timestamp = cpu_to_be64(ktime_get_real_seconds());
406 if (record->compressed)
407 err_type = ERR_TYPE_KERNEL_PANIC_GZ;
409 rc = nvram_write_os_partition(&oops_log_partition, oops_buf,
410 (int) (sizeof(*oops_hdr) + record->size), err_type,
416 record->id = record->part;
421 * Reads the oops/panic report, rtas, of-config and common partition.
422 * Returns the length of the data we read from each partition.
423 * Returns 0 if we've been called before.
425 static ssize_t nvram_pstore_read(struct pstore_record *record)
427 struct oops_log_info *oops_hdr;
428 unsigned int err_type, id_no, size = 0;
429 struct nvram_os_partition *part = NULL;
436 switch (nvram_type_ids[read_type]) {
437 case PSTORE_TYPE_DMESG:
438 part = &oops_log_partition;
439 record->type = PSTORE_TYPE_DMESG;
441 case PSTORE_TYPE_PPC_COMMON:
443 part = &common_partition;
444 record->type = PSTORE_TYPE_PPC_COMMON;
445 record->id = PSTORE_TYPE_PPC_COMMON;
446 record->time.tv_sec = 0;
447 record->time.tv_nsec = 0;
449 #ifdef CONFIG_PPC_PSERIES
450 case PSTORE_TYPE_PPC_RTAS:
451 part = &rtas_log_partition;
452 record->type = PSTORE_TYPE_PPC_RTAS;
453 record->time.tv_sec = last_rtas_event;
454 record->time.tv_nsec = 0;
456 case PSTORE_TYPE_PPC_OF:
458 part = &of_config_partition;
459 record->type = PSTORE_TYPE_PPC_OF;
460 record->id = PSTORE_TYPE_PPC_OF;
461 record->time.tv_sec = 0;
462 record->time.tv_nsec = 0;
465 #ifdef CONFIG_PPC_POWERNV
466 case PSTORE_TYPE_PPC_OPAL:
468 part = &skiboot_partition;
469 record->type = PSTORE_TYPE_PPC_OPAL;
470 record->id = PSTORE_TYPE_PPC_OPAL;
471 record->time.tv_sec = 0;
472 record->time.tv_nsec = 0;
479 if (!part->os_partition) {
480 p = nvram_find_partition(part->name, sig, &size);
482 pr_err("nvram: Failed to find partition %s, "
483 "err %d\n", part->name, (int)p);
490 buff = kmalloc(part->size, GFP_KERNEL);
495 if (nvram_read_partition(part, buff, part->size, &err_type, &id_no)) {
502 if (part->os_partition)
505 if (nvram_type_ids[read_type] == PSTORE_TYPE_DMESG) {
506 size_t length, hdr_size;
508 oops_hdr = (struct oops_log_info *)buff;
509 if (be16_to_cpu(oops_hdr->version) < OOPS_HDR_VERSION) {
510 /* Old format oops header had 2-byte record size */
511 hdr_size = sizeof(u16);
512 length = be16_to_cpu(oops_hdr->version);
513 record->time.tv_sec = 0;
514 record->time.tv_nsec = 0;
516 hdr_size = sizeof(*oops_hdr);
517 length = be16_to_cpu(oops_hdr->report_length);
518 record->time.tv_sec = be64_to_cpu(oops_hdr->timestamp);
519 record->time.tv_nsec = 0;
521 record->buf = kmemdup(buff + hdr_size, length, GFP_KERNEL);
523 if (record->buf == NULL)
526 record->ecc_notice_size = 0;
527 if (err_type == ERR_TYPE_KERNEL_PANIC_GZ)
528 record->compressed = true;
530 record->compressed = false;
538 static struct pstore_info nvram_pstore_info = {
539 .owner = THIS_MODULE,
541 .flags = PSTORE_FLAGS_DMESG,
542 .open = nvram_pstore_open,
543 .read = nvram_pstore_read,
544 .write = nvram_pstore_write,
547 static int nvram_pstore_init(void)
551 if (machine_is(pseries)) {
552 nvram_type_ids[2] = PSTORE_TYPE_PPC_RTAS;
553 nvram_type_ids[3] = PSTORE_TYPE_PPC_OF;
555 nvram_type_ids[2] = PSTORE_TYPE_PPC_OPAL;
557 nvram_pstore_info.buf = oops_data;
558 nvram_pstore_info.bufsize = oops_data_sz;
560 rc = pstore_register(&nvram_pstore_info);
561 if (rc && (rc != -EPERM))
562 /* Print error only when pstore.backend == nvram */
563 pr_err("nvram: pstore_register() failed, returned %d. "
564 "Defaults to kmsg_dump\n", rc);
569 static int nvram_pstore_init(void)
575 void __init nvram_init_oops_partition(int rtas_partition_exists)
579 rc = nvram_init_os_partition(&oops_log_partition);
581 #ifdef CONFIG_PPC_PSERIES
582 if (!rtas_partition_exists) {
583 pr_err("nvram: Failed to initialize oops partition!");
586 pr_notice("nvram: Using %s partition to log both"
587 " RTAS errors and oops/panic reports\n",
588 rtas_log_partition.name);
589 memcpy(&oops_log_partition, &rtas_log_partition,
590 sizeof(rtas_log_partition));
592 pr_err("nvram: Failed to initialize oops partition!");
596 oops_buf = kmalloc(oops_log_partition.size, GFP_KERNEL);
598 pr_err("nvram: No memory for %s partition\n",
599 oops_log_partition.name);
602 oops_data = oops_buf + sizeof(struct oops_log_info);
603 oops_data_sz = oops_log_partition.size - sizeof(struct oops_log_info);
605 rc = nvram_pstore_init();
611 * Figure compression (preceded by elimination of each line's <n>
612 * severity prefix) will reduce the oops/panic report to at most
613 * 45% of its original size.
615 big_oops_buf_sz = (oops_data_sz * 100) / 45;
616 big_oops_buf = kmalloc(big_oops_buf_sz, GFP_KERNEL);
618 stream.workspace = kmalloc(zlib_deflate_workspacesize(
619 WINDOW_BITS, MEM_LEVEL), GFP_KERNEL);
620 if (!stream.workspace) {
621 pr_err("nvram: No memory for compression workspace; "
622 "skipping compression of %s partition data\n",
623 oops_log_partition.name);
628 pr_err("No memory for uncompressed %s data; "
629 "skipping compression\n", oops_log_partition.name);
630 stream.workspace = NULL;
633 rc = kmsg_dump_register(&nvram_kmsg_dumper);
635 pr_err("nvram: kmsg_dump_register() failed; returned %d\n", rc);
638 kfree(stream.workspace);
643 * This is our kmsg_dump callback, called after an oops or panic report
644 * has been written to the printk buffer. We want to capture as much
645 * of the printk buffer as possible. First, capture as much as we can
646 * that we think will compress sufficiently to fit in the lnx,oops-log
647 * partition. If that's too much, go back and capture uncompressed text.
649 static void oops_to_nvram(struct kmsg_dumper *dumper,
650 enum kmsg_dump_reason reason)
652 struct oops_log_info *oops_hdr = (struct oops_log_info *)oops_buf;
653 static unsigned int oops_count = 0;
654 static bool panicking = false;
655 static DEFINE_SPINLOCK(lock);
658 unsigned int err_type = ERR_TYPE_KERNEL_PANIC_GZ;
662 case KMSG_DUMP_RESTART:
664 case KMSG_DUMP_POWEROFF:
665 /* These are almost always orderly shutdowns. */
669 case KMSG_DUMP_PANIC:
672 case KMSG_DUMP_EMERG:
674 /* Panic report already captured. */
678 pr_err("%s: ignoring unrecognized KMSG_DUMP_* reason %d\n",
679 __func__, (int) reason);
683 if (clobbering_unread_rtas_event())
686 if (!spin_trylock_irqsave(&lock, flags))
690 kmsg_dump_get_buffer(dumper, false,
691 big_oops_buf, big_oops_buf_sz, &text_len);
692 rc = zip_oops(text_len);
695 kmsg_dump_rewind(dumper);
696 kmsg_dump_get_buffer(dumper, false,
697 oops_data, oops_data_sz, &text_len);
698 err_type = ERR_TYPE_KERNEL_PANIC;
699 oops_hdr->version = cpu_to_be16(OOPS_HDR_VERSION);
700 oops_hdr->report_length = cpu_to_be16(text_len);
701 oops_hdr->timestamp = cpu_to_be64(ktime_get_real_seconds());
704 (void) nvram_write_os_partition(&oops_log_partition, oops_buf,
705 (int) (sizeof(*oops_hdr) + text_len), err_type,
708 spin_unlock_irqrestore(&lock, flags);
712 static void __init nvram_print_partitions(char * label)
714 struct nvram_partition * tmp_part;
716 printk(KERN_WARNING "--------%s---------\n", label);
717 printk(KERN_WARNING "indx\t\tsig\tchks\tlen\tname\n");
718 list_for_each_entry(tmp_part, &nvram_partitions, partition) {
719 printk(KERN_WARNING "%4d \t%02x\t%02x\t%d\t%12.12s\n",
720 tmp_part->index, tmp_part->header.signature,
721 tmp_part->header.checksum, tmp_part->header.length,
722 tmp_part->header.name);
728 static int __init nvram_write_header(struct nvram_partition * part)
732 struct nvram_header phead;
734 memcpy(&phead, &part->header, NVRAM_HEADER_LEN);
735 phead.length = cpu_to_be16(phead.length);
737 tmp_index = part->index;
738 rc = ppc_md.nvram_write((char *)&phead, NVRAM_HEADER_LEN, &tmp_index);
744 static unsigned char __init nvram_checksum(struct nvram_header *p)
746 unsigned int c_sum, c_sum2;
747 unsigned short *sp = (unsigned short *)p->name; /* assume 6 shorts */
748 c_sum = p->signature + p->length + sp[0] + sp[1] + sp[2] + sp[3] + sp[4] + sp[5];
750 /* The sum may have spilled into the 3rd byte. Fold it back. */
751 c_sum = ((c_sum & 0xffff) + (c_sum >> 16)) & 0xffff;
752 /* The sum cannot exceed 2 bytes. Fold it into a checksum */
753 c_sum2 = (c_sum >> 8) + (c_sum << 8);
754 c_sum = ((c_sum + c_sum2) >> 8) & 0xff;
759 * Per the criteria passed via nvram_remove_partition(), should this
760 * partition be removed? 1=remove, 0=keep
762 static int nvram_can_remove_partition(struct nvram_partition *part,
763 const char *name, int sig, const char *exceptions[])
765 if (part->header.signature != sig)
768 if (strncmp(name, part->header.name, 12))
770 } else if (exceptions) {
772 for (except = exceptions; *except; except++) {
773 if (!strncmp(*except, part->header.name, 12))
781 * nvram_remove_partition - Remove one or more partitions in nvram
782 * @name: name of the partition to remove, or NULL for a
783 * signature only match
784 * @sig: signature of the partition(s) to remove
785 * @exceptions: When removing all partitions with a matching signature,
789 int __init nvram_remove_partition(const char *name, int sig,
790 const char *exceptions[])
792 struct nvram_partition *part, *prev, *tmp;
795 list_for_each_entry(part, &nvram_partitions, partition) {
796 if (!nvram_can_remove_partition(part, name, sig, exceptions))
799 /* Make partition a free partition */
800 part->header.signature = NVRAM_SIG_FREE;
801 memset(part->header.name, 'w', 12);
802 part->header.checksum = nvram_checksum(&part->header);
803 rc = nvram_write_header(part);
805 printk(KERN_ERR "nvram_remove_partition: nvram_write failed (%d)\n", rc);
810 /* Merge contiguous ones */
812 list_for_each_entry_safe(part, tmp, &nvram_partitions, partition) {
813 if (part->header.signature != NVRAM_SIG_FREE) {
818 prev->header.length += part->header.length;
819 prev->header.checksum = nvram_checksum(&prev->header);
820 rc = nvram_write_header(prev);
822 printk(KERN_ERR "nvram_remove_partition: nvram_write failed (%d)\n", rc);
825 list_del(&part->partition);
835 * nvram_create_partition - Create a partition in nvram
836 * @name: name of the partition to create
837 * @sig: signature of the partition to create
838 * @req_size: size of data to allocate in bytes
839 * @min_size: minimum acceptable size (0 means req_size)
841 * Returns a negative error code or a positive nvram index
842 * of the beginning of the data area of the newly created
843 * partition. If you provided a min_size smaller than req_size
844 * you need to query for the actual size yourself after the
845 * call using nvram_partition_get_size().
847 loff_t __init nvram_create_partition(const char *name, int sig,
848 int req_size, int min_size)
850 struct nvram_partition *part;
851 struct nvram_partition *new_part;
852 struct nvram_partition *free_part = NULL;
853 static char nv_init_vals[16];
858 BUILD_BUG_ON(NVRAM_BLOCK_LEN != 16);
860 /* Convert sizes from bytes to blocks */
861 req_size = _ALIGN_UP(req_size, NVRAM_BLOCK_LEN) / NVRAM_BLOCK_LEN;
862 min_size = _ALIGN_UP(min_size, NVRAM_BLOCK_LEN) / NVRAM_BLOCK_LEN;
864 /* If no minimum size specified, make it the same as the
869 if (min_size > req_size)
872 /* Now add one block to each for the header */
876 /* Find a free partition that will give us the maximum needed size
877 If can't find one that will give us the minimum size needed */
878 list_for_each_entry(part, &nvram_partitions, partition) {
879 if (part->header.signature != NVRAM_SIG_FREE)
882 if (part->header.length >= req_size) {
887 if (part->header.length > size &&
888 part->header.length >= min_size) {
889 size = part->header.length;
896 /* Create our OS partition */
897 new_part = kzalloc(sizeof(*new_part), GFP_KERNEL);
899 pr_err("%s: kmalloc failed\n", __func__);
903 new_part->index = free_part->index;
904 new_part->header.signature = sig;
905 new_part->header.length = size;
906 memcpy(new_part->header.name, name, strnlen(name, sizeof(new_part->header.name)));
907 new_part->header.checksum = nvram_checksum(&new_part->header);
909 rc = nvram_write_header(new_part);
911 pr_err("%s: nvram_write_header failed (%d)\n", __func__, rc);
915 list_add_tail(&new_part->partition, &free_part->partition);
917 /* Adjust or remove the partition we stole the space from */
918 if (free_part->header.length > size) {
919 free_part->index += size * NVRAM_BLOCK_LEN;
920 free_part->header.length -= size;
921 free_part->header.checksum = nvram_checksum(&free_part->header);
922 rc = nvram_write_header(free_part);
924 pr_err("%s: nvram_write_header failed (%d)\n",
929 list_del(&free_part->partition);
933 /* Clear the new partition */
934 for (tmp_index = new_part->index + NVRAM_HEADER_LEN;
935 tmp_index < ((size - 1) * NVRAM_BLOCK_LEN);
936 tmp_index += NVRAM_BLOCK_LEN) {
937 rc = ppc_md.nvram_write(nv_init_vals, NVRAM_BLOCK_LEN, &tmp_index);
939 pr_err("%s: nvram_write failed (%d)\n",
945 return new_part->index + NVRAM_HEADER_LEN;
949 * nvram_get_partition_size - Get the data size of an nvram partition
950 * @data_index: This is the offset of the start of the data of
951 * the partition. The same value that is returned by
952 * nvram_create_partition().
954 int nvram_get_partition_size(loff_t data_index)
956 struct nvram_partition *part;
958 list_for_each_entry(part, &nvram_partitions, partition) {
959 if (part->index + NVRAM_HEADER_LEN == data_index)
960 return (part->header.length - 1) * NVRAM_BLOCK_LEN;
967 * nvram_find_partition - Find an nvram partition by signature and name
968 * @name: Name of the partition or NULL for any name
969 * @sig: Signature to test against
970 * @out_size: if non-NULL, returns the size of the data part of the partition
972 loff_t nvram_find_partition(const char *name, int sig, int *out_size)
974 struct nvram_partition *p;
976 list_for_each_entry(p, &nvram_partitions, partition) {
977 if (p->header.signature == sig &&
978 (!name || !strncmp(p->header.name, name, 12))) {
980 *out_size = (p->header.length - 1) *
982 return p->index + NVRAM_HEADER_LEN;
988 int __init nvram_scan_partitions(void)
990 loff_t cur_index = 0;
991 struct nvram_header phead;
992 struct nvram_partition * tmp_part;
998 if (ppc_md.nvram_size == NULL || ppc_md.nvram_size() <= 0)
1000 total_size = ppc_md.nvram_size();
1002 header = kmalloc(NVRAM_HEADER_LEN, GFP_KERNEL);
1004 printk(KERN_ERR "nvram_scan_partitions: Failed kmalloc\n");
1008 while (cur_index < total_size) {
1010 err = ppc_md.nvram_read(header, NVRAM_HEADER_LEN, &cur_index);
1011 if (err != NVRAM_HEADER_LEN) {
1012 printk(KERN_ERR "nvram_scan_partitions: Error parsing "
1013 "nvram partitions\n");
1017 cur_index -= NVRAM_HEADER_LEN; /* nvram_read will advance us */
1019 memcpy(&phead, header, NVRAM_HEADER_LEN);
1021 phead.length = be16_to_cpu(phead.length);
1024 c_sum = nvram_checksum(&phead);
1025 if (c_sum != phead.checksum) {
1026 printk(KERN_WARNING "WARNING: nvram partition checksum"
1027 " was %02x, should be %02x!\n",
1028 phead.checksum, c_sum);
1029 printk(KERN_WARNING "Terminating nvram partition scan\n");
1032 if (!phead.length) {
1033 printk(KERN_WARNING "WARNING: nvram corruption "
1034 "detected: 0-length partition\n");
1037 tmp_part = kmalloc(sizeof(*tmp_part), GFP_KERNEL);
1040 printk(KERN_ERR "nvram_scan_partitions: kmalloc failed\n");
1044 memcpy(&tmp_part->header, &phead, NVRAM_HEADER_LEN);
1045 tmp_part->index = cur_index;
1046 list_add_tail(&tmp_part->partition, &nvram_partitions);
1048 cur_index += phead.length * NVRAM_BLOCK_LEN;
1053 nvram_print_partitions("NVRAM Partitions");