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
11 * This perhaps should live in drivers/char
13 * TODO: Split the /dev/nvram part (that one can use
14 * drivers/char/generic_nvram.c) from the arch & partition
18 #include <linux/types.h>
19 #include <linux/errno.h>
21 #include <linux/miscdevice.h>
22 #include <linux/fcntl.h>
23 #include <linux/nvram.h>
24 #include <linux/init.h>
25 #include <linux/slab.h>
26 #include <linux/spinlock.h>
27 #include <linux/kmsg_dump.h>
28 #include <linux/pagemap.h>
29 #include <linux/pstore.h>
30 #include <linux/zlib.h>
31 #include <asm/uaccess.h>
32 #include <asm/nvram.h>
35 #include <asm/machdep.h>
39 #define NVRAM_HEADER_LEN sizeof(struct nvram_header)
40 #define NVRAM_BLOCK_LEN NVRAM_HEADER_LEN
42 /* If change this size, then change the size of NVNAME_LEN */
44 unsigned char signature
;
45 unsigned char checksum
;
46 unsigned short length
;
47 /* Terminating null required only for names < 12 chars. */
51 struct nvram_partition
{
52 struct list_head partition
;
53 struct nvram_header header
;
57 static LIST_HEAD(nvram_partitions
);
59 #ifdef CONFIG_PPC_PSERIES
60 struct nvram_os_partition rtas_log_partition
= {
61 .name
= "ibm,rtas-log",
69 struct nvram_os_partition oops_log_partition
= {
70 .name
= "lnx,oops-log",
77 static const char *nvram_os_partitions
[] = {
78 #ifdef CONFIG_PPC_PSERIES
85 static void oops_to_nvram(struct kmsg_dumper
*dumper
,
86 enum kmsg_dump_reason reason
);
88 static struct kmsg_dumper nvram_kmsg_dumper
= {
93 * For capturing and compressing an oops or panic report...
95 * big_oops_buf[] holds the uncompressed text we're capturing.
97 * oops_buf[] holds the compressed text, preceded by a oops header.
98 * oops header has u16 holding the version of oops header (to differentiate
99 * between old and new format header) followed by u16 holding the length of
100 * the compressed* text (*Or uncompressed, if compression fails.) and u64
101 * holding the timestamp. oops_buf[] gets written to NVRAM.
103 * oops_log_info points to the header. oops_data points to the compressed text.
108 * +-----------+-----------+-----------+------------------------+
109 * | version | length | timestamp | text |
110 * | (2 bytes) | (2 bytes) | (8 bytes) | (oops_data_sz bytes) |
111 * +-----------+-----------+-----------+------------------------+
115 * We preallocate these buffers during init to avoid kmalloc during oops/panic.
117 static size_t big_oops_buf_sz
;
118 static char *big_oops_buf
, *oops_buf
;
119 static char *oops_data
;
120 static size_t oops_data_sz
;
122 /* Compression parameters */
123 #define COMPR_LEVEL 6
124 #define WINDOW_BITS 12
126 static struct z_stream_s stream
;
129 #ifdef CONFIG_PPC_POWERNV
130 static struct nvram_os_partition skiboot_partition
= {
131 .name
= "ibm,skiboot",
133 .os_partition
= false
137 #ifdef CONFIG_PPC_PSERIES
138 static struct nvram_os_partition of_config_partition
= {
141 .os_partition
= false
145 static struct nvram_os_partition common_partition
= {
148 .os_partition
= false
151 static enum pstore_type_id nvram_type_ids
[] = {
153 PSTORE_TYPE_PPC_COMMON
,
158 static int read_type
;
161 /* nvram_write_os_partition
163 * We need to buffer the error logs into nvram to ensure that we have
164 * the failure information to decode. If we have a severe error there
165 * is no way to guarantee that the OS or the machine is in a state to
166 * get back to user land and write the error to disk. For example if
167 * the SCSI device driver causes a Machine Check by writing to a bad
168 * IO address, there is no way of guaranteeing that the device driver
169 * is in any state that is would also be able to write the error data
170 * captured to disk, thus we buffer it in NVRAM for analysis on the
173 * In NVRAM the partition containing the error log buffer will looks like:
175 * +-----------+----------+--------+------------+------------------+
176 * | signature | checksum | length | name | data |
177 * |0 |1 |2 3|4 15|16 length-1|
178 * +-----------+----------+--------+------------+------------------+
180 * The 'data' section would look like (in bytes):
181 * +--------------+------------+-----------------------------------+
182 * | event_logged | sequence # | error log |
183 * |0 3|4 7|8 error_log_size-1|
184 * +--------------+------------+-----------------------------------+
186 * event_logged: 0 if event has not been logged to syslog, 1 if it has
187 * sequence #: The unique sequence # for each event. (until it wraps)
188 * error log: The error log from event_scan
190 int nvram_write_os_partition(struct nvram_os_partition
*part
,
191 char *buff
, int length
,
192 unsigned int err_type
,
193 unsigned int error_log_cnt
)
197 struct err_log_info info
;
199 if (part
->index
== -1)
202 if (length
> part
->size
)
205 info
.error_type
= cpu_to_be32(err_type
);
206 info
.seq_num
= cpu_to_be32(error_log_cnt
);
208 tmp_index
= part
->index
;
210 rc
= ppc_md
.nvram_write((char *)&info
, sizeof(struct err_log_info
),
213 pr_err("%s: Failed nvram_write (%d)\n", __func__
, rc
);
217 rc
= ppc_md
.nvram_write(buff
, length
, &tmp_index
);
219 pr_err("%s: Failed nvram_write (%d)\n", __func__
, rc
);
226 /* nvram_read_partition
228 * Reads nvram partition for at most 'length'
230 int nvram_read_partition(struct nvram_os_partition
*part
, char *buff
,
231 int length
, unsigned int *err_type
,
232 unsigned int *error_log_cnt
)
236 struct err_log_info info
;
238 if (part
->index
== -1)
241 if (length
> part
->size
)
244 tmp_index
= part
->index
;
246 if (part
->os_partition
) {
247 rc
= ppc_md
.nvram_read((char *)&info
,
248 sizeof(struct err_log_info
),
251 pr_err("%s: Failed nvram_read (%d)\n", __func__
, rc
);
256 rc
= ppc_md
.nvram_read(buff
, length
, &tmp_index
);
258 pr_err("%s: Failed nvram_read (%d)\n", __func__
, rc
);
262 if (part
->os_partition
) {
263 *error_log_cnt
= be32_to_cpu(info
.seq_num
);
264 *err_type
= be32_to_cpu(info
.error_type
);
270 /* nvram_init_os_partition
272 * This sets up a partition with an "OS" signature.
274 * The general strategy is the following:
275 * 1.) If a partition with the indicated name already exists...
276 * - If it's large enough, use it.
277 * - Otherwise, recycle it and keep going.
278 * 2.) Search for a free partition that is large enough.
279 * 3.) If there's not a free partition large enough, recycle any obsolete
280 * OS partitions and try again.
281 * 4.) Will first try getting a chunk that will satisfy the requested size.
282 * 5.) If a chunk of the requested size cannot be allocated, then try finding
283 * a chunk that will satisfy the minum needed.
285 * Returns 0 on success, else -1.
287 int __init
nvram_init_os_partition(struct nvram_os_partition
*part
)
293 p
= nvram_find_partition(part
->name
, NVRAM_SIG_OS
, &size
);
295 /* Found one but too small, remove it */
296 if (p
&& size
< part
->min_size
) {
297 pr_info("nvram: Found too small %s partition,"
298 " removing it...\n", part
->name
);
299 nvram_remove_partition(part
->name
, NVRAM_SIG_OS
, NULL
);
303 /* Create one if we didn't find */
305 p
= nvram_create_partition(part
->name
, NVRAM_SIG_OS
,
306 part
->req_size
, part
->min_size
);
308 pr_info("nvram: No room to create %s partition, "
309 "deleting any obsolete OS partitions...\n",
311 nvram_remove_partition(NULL
, NVRAM_SIG_OS
,
312 nvram_os_partitions
);
313 p
= nvram_create_partition(part
->name
, NVRAM_SIG_OS
,
314 part
->req_size
, part
->min_size
);
319 pr_err("nvram: Failed to find or create %s"
320 " partition, err %d\n", part
->name
, (int)p
);
325 part
->size
= nvram_get_partition_size(p
) - sizeof(struct err_log_info
);
330 /* Derived from logfs_compress() */
331 static int nvram_compress(const void *in
, void *out
, size_t inlen
,
337 err
= zlib_deflateInit2(&stream
, COMPR_LEVEL
, Z_DEFLATED
, WINDOW_BITS
,
338 MEM_LEVEL
, Z_DEFAULT_STRATEGY
);
343 stream
.avail_in
= inlen
;
345 stream
.next_out
= out
;
346 stream
.avail_out
= outlen
;
347 stream
.total_out
= 0;
349 err
= zlib_deflate(&stream
, Z_FINISH
);
350 if (err
!= Z_STREAM_END
)
353 err
= zlib_deflateEnd(&stream
);
357 if (stream
.total_out
>= stream
.total_in
)
360 ret
= stream
.total_out
;
365 /* Compress the text from big_oops_buf into oops_buf. */
366 static int zip_oops(size_t text_len
)
368 struct oops_log_info
*oops_hdr
= (struct oops_log_info
*)oops_buf
;
369 int zipped_len
= nvram_compress(big_oops_buf
, oops_data
, text_len
,
371 if (zipped_len
< 0) {
372 pr_err("nvram: compression failed; returned %d\n", zipped_len
);
373 pr_err("nvram: logging uncompressed oops/panic report\n");
376 oops_hdr
->version
= cpu_to_be16(OOPS_HDR_VERSION
);
377 oops_hdr
->report_length
= cpu_to_be16(zipped_len
);
378 oops_hdr
->timestamp
= cpu_to_be64(ktime_get_real_seconds());
383 static int nvram_pstore_open(struct pstore_info
*psi
)
385 /* Reset the iterator to start reading partitions again */
391 * nvram_pstore_write - pstore write callback for nvram
392 * @type: Type of message logged
393 * @reason: reason behind dump (oops/panic)
394 * @id: identifier to indicate the write performed
395 * @part: pstore writes data to registered buffer in parts,
396 * part number will indicate the same.
397 * @count: Indicates oops count
398 * @compressed: Flag to indicate the log is compressed
399 * @size: number of bytes written to the registered buffer
400 * @psi: registered pstore_info structure
402 * Called by pstore_dump() when an oops or panic report is logged in the
404 * Returns 0 on successful write.
406 static int nvram_pstore_write(enum pstore_type_id type
,
407 enum kmsg_dump_reason reason
,
408 u64
*id
, unsigned int part
, int count
,
409 bool compressed
, size_t size
,
410 struct pstore_info
*psi
)
413 unsigned int err_type
= ERR_TYPE_KERNEL_PANIC
;
414 struct oops_log_info
*oops_hdr
= (struct oops_log_info
*) oops_buf
;
416 /* part 1 has the recent messages from printk buffer */
417 if (part
> 1 || (type
!= PSTORE_TYPE_DMESG
))
420 if (clobbering_unread_rtas_event())
423 oops_hdr
->version
= cpu_to_be16(OOPS_HDR_VERSION
);
424 oops_hdr
->report_length
= cpu_to_be16(size
);
425 oops_hdr
->timestamp
= cpu_to_be64(ktime_get_real_seconds());
428 err_type
= ERR_TYPE_KERNEL_PANIC_GZ
;
430 rc
= nvram_write_os_partition(&oops_log_partition
, oops_buf
,
431 (int) (sizeof(*oops_hdr
) + size
), err_type
, count
);
441 * Reads the oops/panic report, rtas, of-config and common partition.
442 * Returns the length of the data we read from each partition.
443 * Returns 0 if we've been called before.
445 static ssize_t
nvram_pstore_read(u64
*id
, enum pstore_type_id
*type
,
446 int *count
, struct timespec
*time
, char **buf
,
447 bool *compressed
, ssize_t
*ecc_notice_size
,
448 struct pstore_info
*psi
)
450 struct oops_log_info
*oops_hdr
;
451 unsigned int err_type
, id_no
, size
= 0;
452 struct nvram_os_partition
*part
= NULL
;
459 switch (nvram_type_ids
[read_type
]) {
460 case PSTORE_TYPE_DMESG
:
461 part
= &oops_log_partition
;
462 *type
= PSTORE_TYPE_DMESG
;
464 case PSTORE_TYPE_PPC_COMMON
:
466 part
= &common_partition
;
467 *type
= PSTORE_TYPE_PPC_COMMON
;
468 *id
= PSTORE_TYPE_PPC_COMMON
;
472 #ifdef CONFIG_PPC_PSERIES
473 case PSTORE_TYPE_PPC_RTAS
:
474 part
= &rtas_log_partition
;
475 *type
= PSTORE_TYPE_PPC_RTAS
;
476 time
->tv_sec
= last_rtas_event
;
479 case PSTORE_TYPE_PPC_OF
:
481 part
= &of_config_partition
;
482 *type
= PSTORE_TYPE_PPC_OF
;
483 *id
= PSTORE_TYPE_PPC_OF
;
488 #ifdef CONFIG_PPC_POWERNV
489 case PSTORE_TYPE_PPC_OPAL
:
491 part
= &skiboot_partition
;
492 *type
= PSTORE_TYPE_PPC_OPAL
;
493 *id
= PSTORE_TYPE_PPC_OPAL
;
502 if (!part
->os_partition
) {
503 p
= nvram_find_partition(part
->name
, sig
, &size
);
505 pr_err("nvram: Failed to find partition %s, "
506 "err %d\n", part
->name
, (int)p
);
513 buff
= kmalloc(part
->size
, GFP_KERNEL
);
518 if (nvram_read_partition(part
, buff
, part
->size
, &err_type
, &id_no
)) {
525 if (part
->os_partition
)
528 if (nvram_type_ids
[read_type
] == PSTORE_TYPE_DMESG
) {
529 size_t length
, hdr_size
;
531 oops_hdr
= (struct oops_log_info
*)buff
;
532 if (be16_to_cpu(oops_hdr
->version
) < OOPS_HDR_VERSION
) {
533 /* Old format oops header had 2-byte record size */
534 hdr_size
= sizeof(u16
);
535 length
= be16_to_cpu(oops_hdr
->version
);
539 hdr_size
= sizeof(*oops_hdr
);
540 length
= be16_to_cpu(oops_hdr
->report_length
);
541 time
->tv_sec
= be64_to_cpu(oops_hdr
->timestamp
);
544 *buf
= kmemdup(buff
+ hdr_size
, length
, GFP_KERNEL
);
549 *ecc_notice_size
= 0;
550 if (err_type
== ERR_TYPE_KERNEL_PANIC_GZ
)
561 static struct pstore_info nvram_pstore_info
= {
562 .owner
= THIS_MODULE
,
564 .flags
= PSTORE_FLAGS_DMESG
,
565 .open
= nvram_pstore_open
,
566 .read
= nvram_pstore_read
,
567 .write
= nvram_pstore_write
,
570 static int nvram_pstore_init(void)
574 if (machine_is(pseries
)) {
575 nvram_type_ids
[2] = PSTORE_TYPE_PPC_RTAS
;
576 nvram_type_ids
[3] = PSTORE_TYPE_PPC_OF
;
578 nvram_type_ids
[2] = PSTORE_TYPE_PPC_OPAL
;
580 nvram_pstore_info
.buf
= oops_data
;
581 nvram_pstore_info
.bufsize
= oops_data_sz
;
583 spin_lock_init(&nvram_pstore_info
.buf_lock
);
585 rc
= pstore_register(&nvram_pstore_info
);
586 if (rc
&& (rc
!= -EPERM
))
587 /* Print error only when pstore.backend == nvram */
588 pr_err("nvram: pstore_register() failed, returned %d. "
589 "Defaults to kmsg_dump\n", rc
);
594 static int nvram_pstore_init(void)
600 void __init
nvram_init_oops_partition(int rtas_partition_exists
)
604 rc
= nvram_init_os_partition(&oops_log_partition
);
606 #ifdef CONFIG_PPC_PSERIES
607 if (!rtas_partition_exists
) {
608 pr_err("nvram: Failed to initialize oops partition!");
611 pr_notice("nvram: Using %s partition to log both"
612 " RTAS errors and oops/panic reports\n",
613 rtas_log_partition
.name
);
614 memcpy(&oops_log_partition
, &rtas_log_partition
,
615 sizeof(rtas_log_partition
));
617 pr_err("nvram: Failed to initialize oops partition!");
621 oops_buf
= kmalloc(oops_log_partition
.size
, GFP_KERNEL
);
623 pr_err("nvram: No memory for %s partition\n",
624 oops_log_partition
.name
);
627 oops_data
= oops_buf
+ sizeof(struct oops_log_info
);
628 oops_data_sz
= oops_log_partition
.size
- sizeof(struct oops_log_info
);
630 rc
= nvram_pstore_init();
636 * Figure compression (preceded by elimination of each line's <n>
637 * severity prefix) will reduce the oops/panic report to at most
638 * 45% of its original size.
640 big_oops_buf_sz
= (oops_data_sz
* 100) / 45;
641 big_oops_buf
= kmalloc(big_oops_buf_sz
, GFP_KERNEL
);
643 stream
.workspace
= kmalloc(zlib_deflate_workspacesize(
644 WINDOW_BITS
, MEM_LEVEL
), GFP_KERNEL
);
645 if (!stream
.workspace
) {
646 pr_err("nvram: No memory for compression workspace; "
647 "skipping compression of %s partition data\n",
648 oops_log_partition
.name
);
653 pr_err("No memory for uncompressed %s data; "
654 "skipping compression\n", oops_log_partition
.name
);
655 stream
.workspace
= NULL
;
658 rc
= kmsg_dump_register(&nvram_kmsg_dumper
);
660 pr_err("nvram: kmsg_dump_register() failed; returned %d\n", rc
);
663 kfree(stream
.workspace
);
668 * This is our kmsg_dump callback, called after an oops or panic report
669 * has been written to the printk buffer. We want to capture as much
670 * of the printk buffer as possible. First, capture as much as we can
671 * that we think will compress sufficiently to fit in the lnx,oops-log
672 * partition. If that's too much, go back and capture uncompressed text.
674 static void oops_to_nvram(struct kmsg_dumper
*dumper
,
675 enum kmsg_dump_reason reason
)
677 struct oops_log_info
*oops_hdr
= (struct oops_log_info
*)oops_buf
;
678 static unsigned int oops_count
= 0;
679 static bool panicking
= false;
680 static DEFINE_SPINLOCK(lock
);
683 unsigned int err_type
= ERR_TYPE_KERNEL_PANIC_GZ
;
687 case KMSG_DUMP_RESTART
:
689 case KMSG_DUMP_POWEROFF
:
690 /* These are almost always orderly shutdowns. */
694 case KMSG_DUMP_PANIC
:
697 case KMSG_DUMP_EMERG
:
699 /* Panic report already captured. */
703 pr_err("%s: ignoring unrecognized KMSG_DUMP_* reason %d\n",
704 __func__
, (int) reason
);
708 if (clobbering_unread_rtas_event())
711 if (!spin_trylock_irqsave(&lock
, flags
))
715 kmsg_dump_get_buffer(dumper
, false,
716 big_oops_buf
, big_oops_buf_sz
, &text_len
);
717 rc
= zip_oops(text_len
);
720 kmsg_dump_rewind(dumper
);
721 kmsg_dump_get_buffer(dumper
, false,
722 oops_data
, oops_data_sz
, &text_len
);
723 err_type
= ERR_TYPE_KERNEL_PANIC
;
724 oops_hdr
->version
= cpu_to_be16(OOPS_HDR_VERSION
);
725 oops_hdr
->report_length
= cpu_to_be16(text_len
);
726 oops_hdr
->timestamp
= cpu_to_be64(ktime_get_real_seconds());
729 (void) nvram_write_os_partition(&oops_log_partition
, oops_buf
,
730 (int) (sizeof(*oops_hdr
) + text_len
), err_type
,
733 spin_unlock_irqrestore(&lock
, flags
);
736 static loff_t
dev_nvram_llseek(struct file
*file
, loff_t offset
, int origin
)
738 if (ppc_md
.nvram_size
== NULL
)
740 return generic_file_llseek_size(file
, offset
, origin
, MAX_LFS_FILESIZE
,
741 ppc_md
.nvram_size());
745 static ssize_t
dev_nvram_read(struct file
*file
, char __user
*buf
,
746 size_t count
, loff_t
*ppos
)
752 if (!ppc_md
.nvram_size
) {
757 size
= ppc_md
.nvram_size();
768 count
= min_t(size_t, count
, size
- *ppos
);
769 count
= min(count
, PAGE_SIZE
);
771 tmp
= kmalloc(count
, GFP_KERNEL
);
777 ret
= ppc_md
.nvram_read(tmp
, count
, ppos
);
781 if (copy_to_user(buf
, tmp
, ret
))
790 static ssize_t
dev_nvram_write(struct file
*file
, const char __user
*buf
,
791 size_t count
, loff_t
*ppos
)
798 if (!ppc_md
.nvram_size
)
802 size
= ppc_md
.nvram_size();
803 if (*ppos
>= size
|| size
< 0)
806 count
= min_t(size_t, count
, size
- *ppos
);
807 count
= min(count
, PAGE_SIZE
);
810 tmp
= kmalloc(count
, GFP_KERNEL
);
815 if (copy_from_user(tmp
, buf
, count
))
818 ret
= ppc_md
.nvram_write(tmp
, count
, ppos
);
826 static long dev_nvram_ioctl(struct file
*file
, unsigned int cmd
,
830 #ifdef CONFIG_PPC_PMAC
831 case OBSOLETE_PMAC_NVRAM_GET_OFFSET
:
832 printk(KERN_WARNING
"nvram: Using obsolete PMAC_NVRAM_GET_OFFSET ioctl\n");
833 case IOC_NVRAM_GET_OFFSET
: {
836 if (!machine_is(powermac
))
838 if (copy_from_user(&part
, (void __user
*)arg
, sizeof(part
)) != 0)
840 if (part
< pmac_nvram_OF
|| part
> pmac_nvram_NR
)
842 offset
= pmac_get_partition(part
);
845 if (copy_to_user((void __user
*)arg
, &offset
, sizeof(offset
)) != 0)
849 #endif /* CONFIG_PPC_PMAC */
855 static const struct file_operations nvram_fops
= {
856 .owner
= THIS_MODULE
,
857 .llseek
= dev_nvram_llseek
,
858 .read
= dev_nvram_read
,
859 .write
= dev_nvram_write
,
860 .unlocked_ioctl
= dev_nvram_ioctl
,
863 static struct miscdevice nvram_dev
= {
871 static void __init
nvram_print_partitions(char * label
)
873 struct nvram_partition
* tmp_part
;
875 printk(KERN_WARNING
"--------%s---------\n", label
);
876 printk(KERN_WARNING
"indx\t\tsig\tchks\tlen\tname\n");
877 list_for_each_entry(tmp_part
, &nvram_partitions
, partition
) {
878 printk(KERN_WARNING
"%4d \t%02x\t%02x\t%d\t%12.12s\n",
879 tmp_part
->index
, tmp_part
->header
.signature
,
880 tmp_part
->header
.checksum
, tmp_part
->header
.length
,
881 tmp_part
->header
.name
);
887 static int __init
nvram_write_header(struct nvram_partition
* part
)
891 struct nvram_header phead
;
893 memcpy(&phead
, &part
->header
, NVRAM_HEADER_LEN
);
894 phead
.length
= cpu_to_be16(phead
.length
);
896 tmp_index
= part
->index
;
897 rc
= ppc_md
.nvram_write((char *)&phead
, NVRAM_HEADER_LEN
, &tmp_index
);
903 static unsigned char __init
nvram_checksum(struct nvram_header
*p
)
905 unsigned int c_sum
, c_sum2
;
906 unsigned short *sp
= (unsigned short *)p
->name
; /* assume 6 shorts */
907 c_sum
= p
->signature
+ p
->length
+ sp
[0] + sp
[1] + sp
[2] + sp
[3] + sp
[4] + sp
[5];
909 /* The sum may have spilled into the 3rd byte. Fold it back. */
910 c_sum
= ((c_sum
& 0xffff) + (c_sum
>> 16)) & 0xffff;
911 /* The sum cannot exceed 2 bytes. Fold it into a checksum */
912 c_sum2
= (c_sum
>> 8) + (c_sum
<< 8);
913 c_sum
= ((c_sum
+ c_sum2
) >> 8) & 0xff;
918 * Per the criteria passed via nvram_remove_partition(), should this
919 * partition be removed? 1=remove, 0=keep
921 static int nvram_can_remove_partition(struct nvram_partition
*part
,
922 const char *name
, int sig
, const char *exceptions
[])
924 if (part
->header
.signature
!= sig
)
927 if (strncmp(name
, part
->header
.name
, 12))
929 } else if (exceptions
) {
931 for (except
= exceptions
; *except
; except
++) {
932 if (!strncmp(*except
, part
->header
.name
, 12))
940 * nvram_remove_partition - Remove one or more partitions in nvram
941 * @name: name of the partition to remove, or NULL for a
942 * signature only match
943 * @sig: signature of the partition(s) to remove
944 * @exceptions: When removing all partitions with a matching signature,
948 int __init
nvram_remove_partition(const char *name
, int sig
,
949 const char *exceptions
[])
951 struct nvram_partition
*part
, *prev
, *tmp
;
954 list_for_each_entry(part
, &nvram_partitions
, partition
) {
955 if (!nvram_can_remove_partition(part
, name
, sig
, exceptions
))
958 /* Make partition a free partition */
959 part
->header
.signature
= NVRAM_SIG_FREE
;
960 memset(part
->header
.name
, 'w', 12);
961 part
->header
.checksum
= nvram_checksum(&part
->header
);
962 rc
= nvram_write_header(part
);
964 printk(KERN_ERR
"nvram_remove_partition: nvram_write failed (%d)\n", rc
);
969 /* Merge contiguous ones */
971 list_for_each_entry_safe(part
, tmp
, &nvram_partitions
, partition
) {
972 if (part
->header
.signature
!= NVRAM_SIG_FREE
) {
977 prev
->header
.length
+= part
->header
.length
;
978 prev
->header
.checksum
= nvram_checksum(&prev
->header
);
979 rc
= nvram_write_header(prev
);
981 printk(KERN_ERR
"nvram_remove_partition: nvram_write failed (%d)\n", rc
);
984 list_del(&part
->partition
);
994 * nvram_create_partition - Create a partition in nvram
995 * @name: name of the partition to create
996 * @sig: signature of the partition to create
997 * @req_size: size of data to allocate in bytes
998 * @min_size: minimum acceptable size (0 means req_size)
1000 * Returns a negative error code or a positive nvram index
1001 * of the beginning of the data area of the newly created
1002 * partition. If you provided a min_size smaller than req_size
1003 * you need to query for the actual size yourself after the
1004 * call using nvram_partition_get_size().
1006 loff_t __init
nvram_create_partition(const char *name
, int sig
,
1007 int req_size
, int min_size
)
1009 struct nvram_partition
*part
;
1010 struct nvram_partition
*new_part
;
1011 struct nvram_partition
*free_part
= NULL
;
1012 static char nv_init_vals
[16];
1017 /* Convert sizes from bytes to blocks */
1018 req_size
= _ALIGN_UP(req_size
, NVRAM_BLOCK_LEN
) / NVRAM_BLOCK_LEN
;
1019 min_size
= _ALIGN_UP(min_size
, NVRAM_BLOCK_LEN
) / NVRAM_BLOCK_LEN
;
1021 /* If no minimum size specified, make it the same as the
1025 min_size
= req_size
;
1026 if (min_size
> req_size
)
1029 /* Now add one block to each for the header */
1033 /* Find a free partition that will give us the maximum needed size
1034 If can't find one that will give us the minimum size needed */
1035 list_for_each_entry(part
, &nvram_partitions
, partition
) {
1036 if (part
->header
.signature
!= NVRAM_SIG_FREE
)
1039 if (part
->header
.length
>= req_size
) {
1044 if (part
->header
.length
> size
&&
1045 part
->header
.length
>= min_size
) {
1046 size
= part
->header
.length
;
1053 /* Create our OS partition */
1054 new_part
= kmalloc(sizeof(*new_part
), GFP_KERNEL
);
1056 pr_err("%s: kmalloc failed\n", __func__
);
1060 new_part
->index
= free_part
->index
;
1061 new_part
->header
.signature
= sig
;
1062 new_part
->header
.length
= size
;
1063 strncpy(new_part
->header
.name
, name
, 12);
1064 new_part
->header
.checksum
= nvram_checksum(&new_part
->header
);
1066 rc
= nvram_write_header(new_part
);
1068 pr_err("%s: nvram_write_header failed (%d)\n", __func__
, rc
);
1072 list_add_tail(&new_part
->partition
, &free_part
->partition
);
1074 /* Adjust or remove the partition we stole the space from */
1075 if (free_part
->header
.length
> size
) {
1076 free_part
->index
+= size
* NVRAM_BLOCK_LEN
;
1077 free_part
->header
.length
-= size
;
1078 free_part
->header
.checksum
= nvram_checksum(&free_part
->header
);
1079 rc
= nvram_write_header(free_part
);
1081 pr_err("%s: nvram_write_header failed (%d)\n",
1086 list_del(&free_part
->partition
);
1090 /* Clear the new partition */
1091 for (tmp_index
= new_part
->index
+ NVRAM_HEADER_LEN
;
1092 tmp_index
< ((size
- 1) * NVRAM_BLOCK_LEN
);
1093 tmp_index
+= NVRAM_BLOCK_LEN
) {
1094 rc
= ppc_md
.nvram_write(nv_init_vals
, NVRAM_BLOCK_LEN
, &tmp_index
);
1096 pr_err("%s: nvram_write failed (%d)\n",
1102 return new_part
->index
+ NVRAM_HEADER_LEN
;
1106 * nvram_get_partition_size - Get the data size of an nvram partition
1107 * @data_index: This is the offset of the start of the data of
1108 * the partition. The same value that is returned by
1109 * nvram_create_partition().
1111 int nvram_get_partition_size(loff_t data_index
)
1113 struct nvram_partition
*part
;
1115 list_for_each_entry(part
, &nvram_partitions
, partition
) {
1116 if (part
->index
+ NVRAM_HEADER_LEN
== data_index
)
1117 return (part
->header
.length
- 1) * NVRAM_BLOCK_LEN
;
1124 * nvram_find_partition - Find an nvram partition by signature and name
1125 * @name: Name of the partition or NULL for any name
1126 * @sig: Signature to test against
1127 * @out_size: if non-NULL, returns the size of the data part of the partition
1129 loff_t
nvram_find_partition(const char *name
, int sig
, int *out_size
)
1131 struct nvram_partition
*p
;
1133 list_for_each_entry(p
, &nvram_partitions
, partition
) {
1134 if (p
->header
.signature
== sig
&&
1135 (!name
|| !strncmp(p
->header
.name
, name
, 12))) {
1137 *out_size
= (p
->header
.length
- 1) *
1139 return p
->index
+ NVRAM_HEADER_LEN
;
1145 int __init
nvram_scan_partitions(void)
1147 loff_t cur_index
= 0;
1148 struct nvram_header phead
;
1149 struct nvram_partition
* tmp_part
;
1150 unsigned char c_sum
;
1155 if (ppc_md
.nvram_size
== NULL
|| ppc_md
.nvram_size() <= 0)
1157 total_size
= ppc_md
.nvram_size();
1159 header
= kmalloc(NVRAM_HEADER_LEN
, GFP_KERNEL
);
1161 printk(KERN_ERR
"nvram_scan_partitions: Failed kmalloc\n");
1165 while (cur_index
< total_size
) {
1167 err
= ppc_md
.nvram_read(header
, NVRAM_HEADER_LEN
, &cur_index
);
1168 if (err
!= NVRAM_HEADER_LEN
) {
1169 printk(KERN_ERR
"nvram_scan_partitions: Error parsing "
1170 "nvram partitions\n");
1174 cur_index
-= NVRAM_HEADER_LEN
; /* nvram_read will advance us */
1176 memcpy(&phead
, header
, NVRAM_HEADER_LEN
);
1178 phead
.length
= be16_to_cpu(phead
.length
);
1181 c_sum
= nvram_checksum(&phead
);
1182 if (c_sum
!= phead
.checksum
) {
1183 printk(KERN_WARNING
"WARNING: nvram partition checksum"
1184 " was %02x, should be %02x!\n",
1185 phead
.checksum
, c_sum
);
1186 printk(KERN_WARNING
"Terminating nvram partition scan\n");
1189 if (!phead
.length
) {
1190 printk(KERN_WARNING
"WARNING: nvram corruption "
1191 "detected: 0-length partition\n");
1194 tmp_part
= kmalloc(sizeof(struct nvram_partition
), GFP_KERNEL
);
1197 printk(KERN_ERR
"nvram_scan_partitions: kmalloc failed\n");
1201 memcpy(&tmp_part
->header
, &phead
, NVRAM_HEADER_LEN
);
1202 tmp_part
->index
= cur_index
;
1203 list_add_tail(&tmp_part
->partition
, &nvram_partitions
);
1205 cur_index
+= phead
.length
* NVRAM_BLOCK_LEN
;
1210 nvram_print_partitions("NVRAM Partitions");
1218 static int __init
nvram_init(void)
1222 BUILD_BUG_ON(NVRAM_BLOCK_LEN
!= 16);
1224 if (ppc_md
.nvram_size
== NULL
|| ppc_md
.nvram_size() <= 0)
1227 rc
= misc_register(&nvram_dev
);
1229 printk(KERN_ERR
"nvram_init: failed to register device\n");
1235 device_initcall(nvram_init
);