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/module.h>
20 #include <linux/types.h>
21 #include <linux/errno.h>
23 #include <linux/miscdevice.h>
24 #include <linux/fcntl.h>
25 #include <linux/nvram.h>
26 #include <linux/init.h>
27 #include <linux/slab.h>
28 #include <linux/spinlock.h>
29 #include <linux/kmsg_dump.h>
30 #include <linux/pstore.h>
31 #include <linux/zlib.h>
32 #include <asm/uaccess.h>
33 #include <asm/nvram.h>
36 #include <asm/machdep.h>
40 #define NVRAM_HEADER_LEN sizeof(struct nvram_header)
41 #define NVRAM_BLOCK_LEN NVRAM_HEADER_LEN
43 /* If change this size, then change the size of NVNAME_LEN */
45 unsigned char signature
;
46 unsigned char checksum
;
47 unsigned short length
;
48 /* Terminating null required only for names < 12 chars. */
52 struct nvram_partition
{
53 struct list_head partition
;
54 struct nvram_header header
;
58 static LIST_HEAD(nvram_partitions
);
60 #ifdef CONFIG_PPC_PSERIES
61 struct nvram_os_partition rtas_log_partition
= {
62 .name
= "ibm,rtas-log",
70 struct nvram_os_partition oops_log_partition
= {
71 .name
= "lnx,oops-log",
78 static const char *nvram_os_partitions
[] = {
79 #ifdef CONFIG_PPC_PSERIES
86 static void oops_to_nvram(struct kmsg_dumper
*dumper
,
87 enum kmsg_dump_reason reason
);
89 static struct kmsg_dumper nvram_kmsg_dumper
= {
94 * For capturing and compressing an oops or panic report...
96 * big_oops_buf[] holds the uncompressed text we're capturing.
98 * oops_buf[] holds the compressed text, preceded by a oops header.
99 * oops header has u16 holding the version of oops header (to differentiate
100 * between old and new format header) followed by u16 holding the length of
101 * the compressed* text (*Or uncompressed, if compression fails.) and u64
102 * holding the timestamp. oops_buf[] gets written to NVRAM.
104 * oops_log_info points to the header. oops_data points to the compressed text.
109 * +-----------+-----------+-----------+------------------------+
110 * | version | length | timestamp | text |
111 * | (2 bytes) | (2 bytes) | (8 bytes) | (oops_data_sz bytes) |
112 * +-----------+-----------+-----------+------------------------+
116 * We preallocate these buffers during init to avoid kmalloc during oops/panic.
118 static size_t big_oops_buf_sz
;
119 static char *big_oops_buf
, *oops_buf
;
120 static char *oops_data
;
121 static size_t oops_data_sz
;
123 /* Compression parameters */
124 #define COMPR_LEVEL 6
125 #define WINDOW_BITS 12
127 static struct z_stream_s stream
;
130 #ifdef CONFIG_PPC_POWERNV
131 static struct nvram_os_partition skiboot_partition
= {
132 .name
= "ibm,skiboot",
134 .os_partition
= false
138 #ifdef CONFIG_PPC_PSERIES
139 static struct nvram_os_partition of_config_partition
= {
142 .os_partition
= false
146 static struct nvram_os_partition common_partition
= {
149 .os_partition
= false
152 static enum pstore_type_id nvram_type_ids
[] = {
154 PSTORE_TYPE_PPC_COMMON
,
159 static int read_type
;
162 /* nvram_write_os_partition
164 * We need to buffer the error logs into nvram to ensure that we have
165 * the failure information to decode. If we have a severe error there
166 * is no way to guarantee that the OS or the machine is in a state to
167 * get back to user land and write the error to disk. For example if
168 * the SCSI device driver causes a Machine Check by writing to a bad
169 * IO address, there is no way of guaranteeing that the device driver
170 * is in any state that is would also be able to write the error data
171 * captured to disk, thus we buffer it in NVRAM for analysis on the
174 * In NVRAM the partition containing the error log buffer will looks like:
176 * +-----------+----------+--------+------------+------------------+
177 * | signature | checksum | length | name | data |
178 * |0 |1 |2 3|4 15|16 length-1|
179 * +-----------+----------+--------+------------+------------------+
181 * The 'data' section would look like (in bytes):
182 * +--------------+------------+-----------------------------------+
183 * | event_logged | sequence # | error log |
184 * |0 3|4 7|8 error_log_size-1|
185 * +--------------+------------+-----------------------------------+
187 * event_logged: 0 if event has not been logged to syslog, 1 if it has
188 * sequence #: The unique sequence # for each event. (until it wraps)
189 * error log: The error log from event_scan
191 int nvram_write_os_partition(struct nvram_os_partition
*part
,
192 char *buff
, int length
,
193 unsigned int err_type
,
194 unsigned int error_log_cnt
)
198 struct err_log_info info
;
200 if (part
->index
== -1)
203 if (length
> part
->size
)
206 info
.error_type
= cpu_to_be32(err_type
);
207 info
.seq_num
= cpu_to_be32(error_log_cnt
);
209 tmp_index
= part
->index
;
211 rc
= ppc_md
.nvram_write((char *)&info
, sizeof(struct err_log_info
),
214 pr_err("%s: Failed nvram_write (%d)\n", __func__
, rc
);
218 rc
= ppc_md
.nvram_write(buff
, length
, &tmp_index
);
220 pr_err("%s: Failed nvram_write (%d)\n", __func__
, rc
);
227 /* nvram_read_partition
229 * Reads nvram partition for at most 'length'
231 int nvram_read_partition(struct nvram_os_partition
*part
, char *buff
,
232 int length
, unsigned int *err_type
,
233 unsigned int *error_log_cnt
)
237 struct err_log_info info
;
239 if (part
->index
== -1)
242 if (length
> part
->size
)
245 tmp_index
= part
->index
;
247 if (part
->os_partition
) {
248 rc
= ppc_md
.nvram_read((char *)&info
,
249 sizeof(struct err_log_info
),
252 pr_err("%s: Failed nvram_read (%d)\n", __func__
, rc
);
257 rc
= ppc_md
.nvram_read(buff
, length
, &tmp_index
);
259 pr_err("%s: Failed nvram_read (%d)\n", __func__
, rc
);
263 if (part
->os_partition
) {
264 *error_log_cnt
= be32_to_cpu(info
.seq_num
);
265 *err_type
= be32_to_cpu(info
.error_type
);
271 /* nvram_init_os_partition
273 * This sets up a partition with an "OS" signature.
275 * The general strategy is the following:
276 * 1.) If a partition with the indicated name already exists...
277 * - If it's large enough, use it.
278 * - Otherwise, recycle it and keep going.
279 * 2.) Search for a free partition that is large enough.
280 * 3.) If there's not a free partition large enough, recycle any obsolete
281 * OS partitions and try again.
282 * 4.) Will first try getting a chunk that will satisfy the requested size.
283 * 5.) If a chunk of the requested size cannot be allocated, then try finding
284 * a chunk that will satisfy the minum needed.
286 * Returns 0 on success, else -1.
288 int __init
nvram_init_os_partition(struct nvram_os_partition
*part
)
294 p
= nvram_find_partition(part
->name
, NVRAM_SIG_OS
, &size
);
296 /* Found one but too small, remove it */
297 if (p
&& size
< part
->min_size
) {
298 pr_info("nvram: Found too small %s partition,"
299 " removing it...\n", part
->name
);
300 nvram_remove_partition(part
->name
, NVRAM_SIG_OS
, NULL
);
304 /* Create one if we didn't find */
306 p
= nvram_create_partition(part
->name
, NVRAM_SIG_OS
,
307 part
->req_size
, part
->min_size
);
309 pr_info("nvram: No room to create %s partition, "
310 "deleting any obsolete OS partitions...\n",
312 nvram_remove_partition(NULL
, NVRAM_SIG_OS
,
313 nvram_os_partitions
);
314 p
= nvram_create_partition(part
->name
, NVRAM_SIG_OS
,
315 part
->req_size
, part
->min_size
);
320 pr_err("nvram: Failed to find or create %s"
321 " partition, err %d\n", part
->name
, (int)p
);
326 part
->size
= nvram_get_partition_size(p
) - sizeof(struct err_log_info
);
331 /* Derived from logfs_compress() */
332 static int nvram_compress(const void *in
, void *out
, size_t inlen
,
338 err
= zlib_deflateInit2(&stream
, COMPR_LEVEL
, Z_DEFLATED
, WINDOW_BITS
,
339 MEM_LEVEL
, Z_DEFAULT_STRATEGY
);
344 stream
.avail_in
= inlen
;
346 stream
.next_out
= out
;
347 stream
.avail_out
= outlen
;
348 stream
.total_out
= 0;
350 err
= zlib_deflate(&stream
, Z_FINISH
);
351 if (err
!= Z_STREAM_END
)
354 err
= zlib_deflateEnd(&stream
);
358 if (stream
.total_out
>= stream
.total_in
)
361 ret
= stream
.total_out
;
366 /* Compress the text from big_oops_buf into oops_buf. */
367 static int zip_oops(size_t text_len
)
369 struct oops_log_info
*oops_hdr
= (struct oops_log_info
*)oops_buf
;
370 int zipped_len
= nvram_compress(big_oops_buf
, oops_data
, text_len
,
372 if (zipped_len
< 0) {
373 pr_err("nvram: compression failed; returned %d\n", zipped_len
);
374 pr_err("nvram: logging uncompressed oops/panic report\n");
377 oops_hdr
->version
= cpu_to_be16(OOPS_HDR_VERSION
);
378 oops_hdr
->report_length
= cpu_to_be16(zipped_len
);
379 oops_hdr
->timestamp
= cpu_to_be64(ktime_get_real_seconds());
384 static int nvram_pstore_open(struct pstore_info
*psi
)
386 /* Reset the iterator to start reading partitions again */
392 * nvram_pstore_write - pstore write callback for nvram
393 * @type: Type of message logged
394 * @reason: reason behind dump (oops/panic)
395 * @id: identifier to indicate the write performed
396 * @part: pstore writes data to registered buffer in parts,
397 * part number will indicate the same.
398 * @count: Indicates oops count
399 * @compressed: Flag to indicate the log is compressed
400 * @size: number of bytes written to the registered buffer
401 * @psi: registered pstore_info structure
403 * Called by pstore_dump() when an oops or panic report is logged in the
405 * Returns 0 on successful write.
407 static int nvram_pstore_write(enum pstore_type_id type
,
408 enum kmsg_dump_reason reason
,
409 u64
*id
, unsigned int part
, int count
,
410 bool compressed
, size_t size
,
411 struct pstore_info
*psi
)
414 unsigned int err_type
= ERR_TYPE_KERNEL_PANIC
;
415 struct oops_log_info
*oops_hdr
= (struct oops_log_info
*) oops_buf
;
417 /* part 1 has the recent messages from printk buffer */
418 if (part
> 1 || (type
!= PSTORE_TYPE_DMESG
))
421 if (clobbering_unread_rtas_event())
424 oops_hdr
->version
= cpu_to_be16(OOPS_HDR_VERSION
);
425 oops_hdr
->report_length
= cpu_to_be16(size
);
426 oops_hdr
->timestamp
= cpu_to_be64(ktime_get_real_seconds());
429 err_type
= ERR_TYPE_KERNEL_PANIC_GZ
;
431 rc
= nvram_write_os_partition(&oops_log_partition
, oops_buf
,
432 (int) (sizeof(*oops_hdr
) + size
), err_type
, count
);
442 * Reads the oops/panic report, rtas, of-config and common partition.
443 * Returns the length of the data we read from each partition.
444 * Returns 0 if we've been called before.
446 static ssize_t
nvram_pstore_read(u64
*id
, enum pstore_type_id
*type
,
447 int *count
, struct timespec
*time
, char **buf
,
448 bool *compressed
, 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 if (err_type
== ERR_TYPE_KERNEL_PANIC_GZ
)
560 static struct pstore_info nvram_pstore_info
= {
561 .owner
= THIS_MODULE
,
563 .open
= nvram_pstore_open
,
564 .read
= nvram_pstore_read
,
565 .write
= nvram_pstore_write
,
568 static int nvram_pstore_init(void)
572 if (machine_is(pseries
)) {
573 nvram_type_ids
[2] = PSTORE_TYPE_PPC_RTAS
;
574 nvram_type_ids
[3] = PSTORE_TYPE_PPC_OF
;
576 nvram_type_ids
[2] = PSTORE_TYPE_PPC_OPAL
;
578 nvram_pstore_info
.buf
= oops_data
;
579 nvram_pstore_info
.bufsize
= oops_data_sz
;
581 spin_lock_init(&nvram_pstore_info
.buf_lock
);
583 rc
= pstore_register(&nvram_pstore_info
);
584 if (rc
&& (rc
!= -EPERM
))
585 /* Print error only when pstore.backend == nvram */
586 pr_err("nvram: pstore_register() failed, returned %d. "
587 "Defaults to kmsg_dump\n", rc
);
592 static int nvram_pstore_init(void)
598 void __init
nvram_init_oops_partition(int rtas_partition_exists
)
602 rc
= nvram_init_os_partition(&oops_log_partition
);
604 #ifdef CONFIG_PPC_PSERIES
605 if (!rtas_partition_exists
) {
606 pr_err("nvram: Failed to initialize oops partition!");
609 pr_notice("nvram: Using %s partition to log both"
610 " RTAS errors and oops/panic reports\n",
611 rtas_log_partition
.name
);
612 memcpy(&oops_log_partition
, &rtas_log_partition
,
613 sizeof(rtas_log_partition
));
615 pr_err("nvram: Failed to initialize oops partition!");
619 oops_buf
= kmalloc(oops_log_partition
.size
, GFP_KERNEL
);
621 pr_err("nvram: No memory for %s partition\n",
622 oops_log_partition
.name
);
625 oops_data
= oops_buf
+ sizeof(struct oops_log_info
);
626 oops_data_sz
= oops_log_partition
.size
- sizeof(struct oops_log_info
);
628 rc
= nvram_pstore_init();
634 * Figure compression (preceded by elimination of each line's <n>
635 * severity prefix) will reduce the oops/panic report to at most
636 * 45% of its original size.
638 big_oops_buf_sz
= (oops_data_sz
* 100) / 45;
639 big_oops_buf
= kmalloc(big_oops_buf_sz
, GFP_KERNEL
);
641 stream
.workspace
= kmalloc(zlib_deflate_workspacesize(
642 WINDOW_BITS
, MEM_LEVEL
), GFP_KERNEL
);
643 if (!stream
.workspace
) {
644 pr_err("nvram: No memory for compression workspace; "
645 "skipping compression of %s partition data\n",
646 oops_log_partition
.name
);
651 pr_err("No memory for uncompressed %s data; "
652 "skipping compression\n", oops_log_partition
.name
);
653 stream
.workspace
= NULL
;
656 rc
= kmsg_dump_register(&nvram_kmsg_dumper
);
658 pr_err("nvram: kmsg_dump_register() failed; returned %d\n", rc
);
661 kfree(stream
.workspace
);
666 * This is our kmsg_dump callback, called after an oops or panic report
667 * has been written to the printk buffer. We want to capture as much
668 * of the printk buffer as possible. First, capture as much as we can
669 * that we think will compress sufficiently to fit in the lnx,oops-log
670 * partition. If that's too much, go back and capture uncompressed text.
672 static void oops_to_nvram(struct kmsg_dumper
*dumper
,
673 enum kmsg_dump_reason reason
)
675 struct oops_log_info
*oops_hdr
= (struct oops_log_info
*)oops_buf
;
676 static unsigned int oops_count
= 0;
677 static bool panicking
= false;
678 static DEFINE_SPINLOCK(lock
);
681 unsigned int err_type
= ERR_TYPE_KERNEL_PANIC_GZ
;
685 case KMSG_DUMP_RESTART
:
687 case KMSG_DUMP_POWEROFF
:
688 /* These are almost always orderly shutdowns. */
692 case KMSG_DUMP_PANIC
:
695 case KMSG_DUMP_EMERG
:
697 /* Panic report already captured. */
701 pr_err("%s: ignoring unrecognized KMSG_DUMP_* reason %d\n",
702 __func__
, (int) reason
);
706 if (clobbering_unread_rtas_event())
709 if (!spin_trylock_irqsave(&lock
, flags
))
713 kmsg_dump_get_buffer(dumper
, false,
714 big_oops_buf
, big_oops_buf_sz
, &text_len
);
715 rc
= zip_oops(text_len
);
718 kmsg_dump_rewind(dumper
);
719 kmsg_dump_get_buffer(dumper
, false,
720 oops_data
, oops_data_sz
, &text_len
);
721 err_type
= ERR_TYPE_KERNEL_PANIC
;
722 oops_hdr
->version
= cpu_to_be16(OOPS_HDR_VERSION
);
723 oops_hdr
->report_length
= cpu_to_be16(text_len
);
724 oops_hdr
->timestamp
= cpu_to_be64(ktime_get_real_seconds());
727 (void) nvram_write_os_partition(&oops_log_partition
, oops_buf
,
728 (int) (sizeof(*oops_hdr
) + text_len
), err_type
,
731 spin_unlock_irqrestore(&lock
, flags
);
734 static loff_t
dev_nvram_llseek(struct file
*file
, loff_t offset
, int origin
)
738 if (ppc_md
.nvram_size
== NULL
)
740 size
= ppc_md
.nvram_size();
744 offset
+= file
->f_pos
;
752 file
->f_pos
= offset
;
757 static ssize_t
dev_nvram_read(struct file
*file
, char __user
*buf
,
758 size_t count
, loff_t
*ppos
)
764 if (!ppc_md
.nvram_size
) {
769 size
= ppc_md
.nvram_size();
780 count
= min_t(size_t, count
, size
- *ppos
);
781 count
= min(count
, PAGE_SIZE
);
783 tmp
= kmalloc(count
, GFP_KERNEL
);
789 ret
= ppc_md
.nvram_read(tmp
, count
, ppos
);
793 if (copy_to_user(buf
, tmp
, ret
))
802 static ssize_t
dev_nvram_write(struct file
*file
, const char __user
*buf
,
803 size_t count
, loff_t
*ppos
)
810 if (!ppc_md
.nvram_size
)
814 size
= ppc_md
.nvram_size();
815 if (*ppos
>= size
|| size
< 0)
818 count
= min_t(size_t, count
, size
- *ppos
);
819 count
= min(count
, PAGE_SIZE
);
822 tmp
= kmalloc(count
, GFP_KERNEL
);
827 if (copy_from_user(tmp
, buf
, count
))
830 ret
= ppc_md
.nvram_write(tmp
, count
, ppos
);
838 static long dev_nvram_ioctl(struct file
*file
, unsigned int cmd
,
842 #ifdef CONFIG_PPC_PMAC
843 case OBSOLETE_PMAC_NVRAM_GET_OFFSET
:
844 printk(KERN_WARNING
"nvram: Using obsolete PMAC_NVRAM_GET_OFFSET ioctl\n");
845 case IOC_NVRAM_GET_OFFSET
: {
848 if (!machine_is(powermac
))
850 if (copy_from_user(&part
, (void __user
*)arg
, sizeof(part
)) != 0)
852 if (part
< pmac_nvram_OF
|| part
> pmac_nvram_NR
)
854 offset
= pmac_get_partition(part
);
857 if (copy_to_user((void __user
*)arg
, &offset
, sizeof(offset
)) != 0)
861 #endif /* CONFIG_PPC_PMAC */
867 const struct file_operations nvram_fops
= {
868 .owner
= THIS_MODULE
,
869 .llseek
= dev_nvram_llseek
,
870 .read
= dev_nvram_read
,
871 .write
= dev_nvram_write
,
872 .unlocked_ioctl
= dev_nvram_ioctl
,
875 static struct miscdevice nvram_dev
= {
883 static void __init
nvram_print_partitions(char * label
)
885 struct nvram_partition
* tmp_part
;
887 printk(KERN_WARNING
"--------%s---------\n", label
);
888 printk(KERN_WARNING
"indx\t\tsig\tchks\tlen\tname\n");
889 list_for_each_entry(tmp_part
, &nvram_partitions
, partition
) {
890 printk(KERN_WARNING
"%4d \t%02x\t%02x\t%d\t%12.12s\n",
891 tmp_part
->index
, tmp_part
->header
.signature
,
892 tmp_part
->header
.checksum
, tmp_part
->header
.length
,
893 tmp_part
->header
.name
);
899 static int __init
nvram_write_header(struct nvram_partition
* part
)
903 struct nvram_header phead
;
905 memcpy(&phead
, &part
->header
, NVRAM_HEADER_LEN
);
906 phead
.length
= cpu_to_be16(phead
.length
);
908 tmp_index
= part
->index
;
909 rc
= ppc_md
.nvram_write((char *)&phead
, NVRAM_HEADER_LEN
, &tmp_index
);
915 static unsigned char __init
nvram_checksum(struct nvram_header
*p
)
917 unsigned int c_sum
, c_sum2
;
918 unsigned short *sp
= (unsigned short *)p
->name
; /* assume 6 shorts */
919 c_sum
= p
->signature
+ p
->length
+ sp
[0] + sp
[1] + sp
[2] + sp
[3] + sp
[4] + sp
[5];
921 /* The sum may have spilled into the 3rd byte. Fold it back. */
922 c_sum
= ((c_sum
& 0xffff) + (c_sum
>> 16)) & 0xffff;
923 /* The sum cannot exceed 2 bytes. Fold it into a checksum */
924 c_sum2
= (c_sum
>> 8) + (c_sum
<< 8);
925 c_sum
= ((c_sum
+ c_sum2
) >> 8) & 0xff;
930 * Per the criteria passed via nvram_remove_partition(), should this
931 * partition be removed? 1=remove, 0=keep
933 static int nvram_can_remove_partition(struct nvram_partition
*part
,
934 const char *name
, int sig
, const char *exceptions
[])
936 if (part
->header
.signature
!= sig
)
939 if (strncmp(name
, part
->header
.name
, 12))
941 } else if (exceptions
) {
943 for (except
= exceptions
; *except
; except
++) {
944 if (!strncmp(*except
, part
->header
.name
, 12))
952 * nvram_remove_partition - Remove one or more partitions in nvram
953 * @name: name of the partition to remove, or NULL for a
954 * signature only match
955 * @sig: signature of the partition(s) to remove
956 * @exceptions: When removing all partitions with a matching signature,
960 int __init
nvram_remove_partition(const char *name
, int sig
,
961 const char *exceptions
[])
963 struct nvram_partition
*part
, *prev
, *tmp
;
966 list_for_each_entry(part
, &nvram_partitions
, partition
) {
967 if (!nvram_can_remove_partition(part
, name
, sig
, exceptions
))
970 /* Make partition a free partition */
971 part
->header
.signature
= NVRAM_SIG_FREE
;
972 strncpy(part
->header
.name
, "wwwwwwwwwwww", 12);
973 part
->header
.checksum
= nvram_checksum(&part
->header
);
974 rc
= nvram_write_header(part
);
976 printk(KERN_ERR
"nvram_remove_partition: nvram_write failed (%d)\n", rc
);
981 /* Merge contiguous ones */
983 list_for_each_entry_safe(part
, tmp
, &nvram_partitions
, partition
) {
984 if (part
->header
.signature
!= NVRAM_SIG_FREE
) {
989 prev
->header
.length
+= part
->header
.length
;
990 prev
->header
.checksum
= nvram_checksum(&part
->header
);
991 rc
= nvram_write_header(part
);
993 printk(KERN_ERR
"nvram_remove_partition: nvram_write failed (%d)\n", rc
);
996 list_del(&part
->partition
);
1006 * nvram_create_partition - Create a partition in nvram
1007 * @name: name of the partition to create
1008 * @sig: signature of the partition to create
1009 * @req_size: size of data to allocate in bytes
1010 * @min_size: minimum acceptable size (0 means req_size)
1012 * Returns a negative error code or a positive nvram index
1013 * of the beginning of the data area of the newly created
1014 * partition. If you provided a min_size smaller than req_size
1015 * you need to query for the actual size yourself after the
1016 * call using nvram_partition_get_size().
1018 loff_t __init
nvram_create_partition(const char *name
, int sig
,
1019 int req_size
, int min_size
)
1021 struct nvram_partition
*part
;
1022 struct nvram_partition
*new_part
;
1023 struct nvram_partition
*free_part
= NULL
;
1024 static char nv_init_vals
[16];
1029 /* Convert sizes from bytes to blocks */
1030 req_size
= _ALIGN_UP(req_size
, NVRAM_BLOCK_LEN
) / NVRAM_BLOCK_LEN
;
1031 min_size
= _ALIGN_UP(min_size
, NVRAM_BLOCK_LEN
) / NVRAM_BLOCK_LEN
;
1033 /* If no minimum size specified, make it the same as the
1037 min_size
= req_size
;
1038 if (min_size
> req_size
)
1041 /* Now add one block to each for the header */
1045 /* Find a free partition that will give us the maximum needed size
1046 If can't find one that will give us the minimum size needed */
1047 list_for_each_entry(part
, &nvram_partitions
, partition
) {
1048 if (part
->header
.signature
!= NVRAM_SIG_FREE
)
1051 if (part
->header
.length
>= req_size
) {
1056 if (part
->header
.length
> size
&&
1057 part
->header
.length
>= min_size
) {
1058 size
= part
->header
.length
;
1065 /* Create our OS partition */
1066 new_part
= kmalloc(sizeof(*new_part
), GFP_KERNEL
);
1068 pr_err("nvram_create_os_partition: kmalloc failed\n");
1072 new_part
->index
= free_part
->index
;
1073 new_part
->header
.signature
= sig
;
1074 new_part
->header
.length
= size
;
1075 strncpy(new_part
->header
.name
, name
, 12);
1076 new_part
->header
.checksum
= nvram_checksum(&new_part
->header
);
1078 rc
= nvram_write_header(new_part
);
1080 pr_err("nvram_create_os_partition: nvram_write_header "
1081 "failed (%d)\n", rc
);
1084 list_add_tail(&new_part
->partition
, &free_part
->partition
);
1086 /* Adjust or remove the partition we stole the space from */
1087 if (free_part
->header
.length
> size
) {
1088 free_part
->index
+= size
* NVRAM_BLOCK_LEN
;
1089 free_part
->header
.length
-= size
;
1090 free_part
->header
.checksum
= nvram_checksum(&free_part
->header
);
1091 rc
= nvram_write_header(free_part
);
1093 pr_err("nvram_create_os_partition: nvram_write_header "
1094 "failed (%d)\n", rc
);
1098 list_del(&free_part
->partition
);
1102 /* Clear the new partition */
1103 for (tmp_index
= new_part
->index
+ NVRAM_HEADER_LEN
;
1104 tmp_index
< ((size
- 1) * NVRAM_BLOCK_LEN
);
1105 tmp_index
+= NVRAM_BLOCK_LEN
) {
1106 rc
= ppc_md
.nvram_write(nv_init_vals
, NVRAM_BLOCK_LEN
, &tmp_index
);
1108 pr_err("nvram_create_partition: nvram_write failed (%d)\n", rc
);
1113 return new_part
->index
+ NVRAM_HEADER_LEN
;
1117 * nvram_get_partition_size - Get the data size of an nvram partition
1118 * @data_index: This is the offset of the start of the data of
1119 * the partition. The same value that is returned by
1120 * nvram_create_partition().
1122 int nvram_get_partition_size(loff_t data_index
)
1124 struct nvram_partition
*part
;
1126 list_for_each_entry(part
, &nvram_partitions
, partition
) {
1127 if (part
->index
+ NVRAM_HEADER_LEN
== data_index
)
1128 return (part
->header
.length
- 1) * NVRAM_BLOCK_LEN
;
1135 * nvram_find_partition - Find an nvram partition by signature and name
1136 * @name: Name of the partition or NULL for any name
1137 * @sig: Signature to test against
1138 * @out_size: if non-NULL, returns the size of the data part of the partition
1140 loff_t
nvram_find_partition(const char *name
, int sig
, int *out_size
)
1142 struct nvram_partition
*p
;
1144 list_for_each_entry(p
, &nvram_partitions
, partition
) {
1145 if (p
->header
.signature
== sig
&&
1146 (!name
|| !strncmp(p
->header
.name
, name
, 12))) {
1148 *out_size
= (p
->header
.length
- 1) *
1150 return p
->index
+ NVRAM_HEADER_LEN
;
1156 int __init
nvram_scan_partitions(void)
1158 loff_t cur_index
= 0;
1159 struct nvram_header phead
;
1160 struct nvram_partition
* tmp_part
;
1161 unsigned char c_sum
;
1166 if (ppc_md
.nvram_size
== NULL
|| ppc_md
.nvram_size() <= 0)
1168 total_size
= ppc_md
.nvram_size();
1170 header
= kmalloc(NVRAM_HEADER_LEN
, GFP_KERNEL
);
1172 printk(KERN_ERR
"nvram_scan_partitions: Failed kmalloc\n");
1176 while (cur_index
< total_size
) {
1178 err
= ppc_md
.nvram_read(header
, NVRAM_HEADER_LEN
, &cur_index
);
1179 if (err
!= NVRAM_HEADER_LEN
) {
1180 printk(KERN_ERR
"nvram_scan_partitions: Error parsing "
1181 "nvram partitions\n");
1185 cur_index
-= NVRAM_HEADER_LEN
; /* nvram_read will advance us */
1187 memcpy(&phead
, header
, NVRAM_HEADER_LEN
);
1189 phead
.length
= be16_to_cpu(phead
.length
);
1192 c_sum
= nvram_checksum(&phead
);
1193 if (c_sum
!= phead
.checksum
) {
1194 printk(KERN_WARNING
"WARNING: nvram partition checksum"
1195 " was %02x, should be %02x!\n",
1196 phead
.checksum
, c_sum
);
1197 printk(KERN_WARNING
"Terminating nvram partition scan\n");
1200 if (!phead
.length
) {
1201 printk(KERN_WARNING
"WARNING: nvram corruption "
1202 "detected: 0-length partition\n");
1205 tmp_part
= kmalloc(sizeof(struct nvram_partition
), GFP_KERNEL
);
1208 printk(KERN_ERR
"nvram_scan_partitions: kmalloc failed\n");
1212 memcpy(&tmp_part
->header
, &phead
, NVRAM_HEADER_LEN
);
1213 tmp_part
->index
= cur_index
;
1214 list_add_tail(&tmp_part
->partition
, &nvram_partitions
);
1216 cur_index
+= phead
.length
* NVRAM_BLOCK_LEN
;
1221 nvram_print_partitions("NVRAM Partitions");
1229 static int __init
nvram_init(void)
1233 BUILD_BUG_ON(NVRAM_BLOCK_LEN
!= 16);
1235 if (ppc_md
.nvram_size
== NULL
|| ppc_md
.nvram_size() <= 0)
1238 rc
= misc_register(&nvram_dev
);
1240 printk(KERN_ERR
"nvram_init: failed to register device\n");
1247 static void __exit
nvram_cleanup(void)
1249 misc_deregister( &nvram_dev
);
1252 module_init(nvram_init
);
1253 module_exit(nvram_cleanup
);
1254 MODULE_LICENSE("GPL");