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/pagemap.h>
31 #include <linux/pstore.h>
32 #include <linux/zlib.h>
33 #include <asm/uaccess.h>
34 #include <asm/nvram.h>
37 #include <asm/machdep.h>
41 #define NVRAM_HEADER_LEN sizeof(struct nvram_header)
42 #define NVRAM_BLOCK_LEN NVRAM_HEADER_LEN
44 /* If change this size, then change the size of NVNAME_LEN */
46 unsigned char signature
;
47 unsigned char checksum
;
48 unsigned short length
;
49 /* Terminating null required only for names < 12 chars. */
53 struct nvram_partition
{
54 struct list_head partition
;
55 struct nvram_header header
;
59 static LIST_HEAD(nvram_partitions
);
61 #ifdef CONFIG_PPC_PSERIES
62 struct nvram_os_partition rtas_log_partition
= {
63 .name
= "ibm,rtas-log",
71 struct nvram_os_partition oops_log_partition
= {
72 .name
= "lnx,oops-log",
79 static const char *nvram_os_partitions
[] = {
80 #ifdef CONFIG_PPC_PSERIES
87 static void oops_to_nvram(struct kmsg_dumper
*dumper
,
88 enum kmsg_dump_reason reason
);
90 static struct kmsg_dumper nvram_kmsg_dumper
= {
95 * For capturing and compressing an oops or panic report...
97 * big_oops_buf[] holds the uncompressed text we're capturing.
99 * oops_buf[] holds the compressed text, preceded by a oops header.
100 * oops header has u16 holding the version of oops header (to differentiate
101 * between old and new format header) followed by u16 holding the length of
102 * the compressed* text (*Or uncompressed, if compression fails.) and u64
103 * holding the timestamp. oops_buf[] gets written to NVRAM.
105 * oops_log_info points to the header. oops_data points to the compressed text.
110 * +-----------+-----------+-----------+------------------------+
111 * | version | length | timestamp | text |
112 * | (2 bytes) | (2 bytes) | (8 bytes) | (oops_data_sz bytes) |
113 * +-----------+-----------+-----------+------------------------+
117 * We preallocate these buffers during init to avoid kmalloc during oops/panic.
119 static size_t big_oops_buf_sz
;
120 static char *big_oops_buf
, *oops_buf
;
121 static char *oops_data
;
122 static size_t oops_data_sz
;
124 /* Compression parameters */
125 #define COMPR_LEVEL 6
126 #define WINDOW_BITS 12
128 static struct z_stream_s stream
;
131 #ifdef CONFIG_PPC_POWERNV
132 static struct nvram_os_partition skiboot_partition
= {
133 .name
= "ibm,skiboot",
135 .os_partition
= false
139 #ifdef CONFIG_PPC_PSERIES
140 static struct nvram_os_partition of_config_partition
= {
143 .os_partition
= false
147 static struct nvram_os_partition common_partition
= {
150 .os_partition
= false
153 static enum pstore_type_id nvram_type_ids
[] = {
155 PSTORE_TYPE_PPC_COMMON
,
160 static int read_type
;
163 /* nvram_write_os_partition
165 * We need to buffer the error logs into nvram to ensure that we have
166 * the failure information to decode. If we have a severe error there
167 * is no way to guarantee that the OS or the machine is in a state to
168 * get back to user land and write the error to disk. For example if
169 * the SCSI device driver causes a Machine Check by writing to a bad
170 * IO address, there is no way of guaranteeing that the device driver
171 * is in any state that is would also be able to write the error data
172 * captured to disk, thus we buffer it in NVRAM for analysis on the
175 * In NVRAM the partition containing the error log buffer will looks like:
177 * +-----------+----------+--------+------------+------------------+
178 * | signature | checksum | length | name | data |
179 * |0 |1 |2 3|4 15|16 length-1|
180 * +-----------+----------+--------+------------+------------------+
182 * The 'data' section would look like (in bytes):
183 * +--------------+------------+-----------------------------------+
184 * | event_logged | sequence # | error log |
185 * |0 3|4 7|8 error_log_size-1|
186 * +--------------+------------+-----------------------------------+
188 * event_logged: 0 if event has not been logged to syslog, 1 if it has
189 * sequence #: The unique sequence # for each event. (until it wraps)
190 * error log: The error log from event_scan
192 int nvram_write_os_partition(struct nvram_os_partition
*part
,
193 char *buff
, int length
,
194 unsigned int err_type
,
195 unsigned int error_log_cnt
)
199 struct err_log_info info
;
201 if (part
->index
== -1)
204 if (length
> part
->size
)
207 info
.error_type
= cpu_to_be32(err_type
);
208 info
.seq_num
= cpu_to_be32(error_log_cnt
);
210 tmp_index
= part
->index
;
212 rc
= ppc_md
.nvram_write((char *)&info
, sizeof(struct err_log_info
),
215 pr_err("%s: Failed nvram_write (%d)\n", __func__
, rc
);
219 rc
= ppc_md
.nvram_write(buff
, length
, &tmp_index
);
221 pr_err("%s: Failed nvram_write (%d)\n", __func__
, rc
);
228 /* nvram_read_partition
230 * Reads nvram partition for at most 'length'
232 int nvram_read_partition(struct nvram_os_partition
*part
, char *buff
,
233 int length
, unsigned int *err_type
,
234 unsigned int *error_log_cnt
)
238 struct err_log_info info
;
240 if (part
->index
== -1)
243 if (length
> part
->size
)
246 tmp_index
= part
->index
;
248 if (part
->os_partition
) {
249 rc
= ppc_md
.nvram_read((char *)&info
,
250 sizeof(struct err_log_info
),
253 pr_err("%s: Failed nvram_read (%d)\n", __func__
, rc
);
258 rc
= ppc_md
.nvram_read(buff
, length
, &tmp_index
);
260 pr_err("%s: Failed nvram_read (%d)\n", __func__
, rc
);
264 if (part
->os_partition
) {
265 *error_log_cnt
= be32_to_cpu(info
.seq_num
);
266 *err_type
= be32_to_cpu(info
.error_type
);
272 /* nvram_init_os_partition
274 * This sets up a partition with an "OS" signature.
276 * The general strategy is the following:
277 * 1.) If a partition with the indicated name already exists...
278 * - If it's large enough, use it.
279 * - Otherwise, recycle it and keep going.
280 * 2.) Search for a free partition that is large enough.
281 * 3.) If there's not a free partition large enough, recycle any obsolete
282 * OS partitions and try again.
283 * 4.) Will first try getting a chunk that will satisfy the requested size.
284 * 5.) If a chunk of the requested size cannot be allocated, then try finding
285 * a chunk that will satisfy the minum needed.
287 * Returns 0 on success, else -1.
289 int __init
nvram_init_os_partition(struct nvram_os_partition
*part
)
295 p
= nvram_find_partition(part
->name
, NVRAM_SIG_OS
, &size
);
297 /* Found one but too small, remove it */
298 if (p
&& size
< part
->min_size
) {
299 pr_info("nvram: Found too small %s partition,"
300 " removing it...\n", part
->name
);
301 nvram_remove_partition(part
->name
, NVRAM_SIG_OS
, NULL
);
305 /* Create one if we didn't find */
307 p
= nvram_create_partition(part
->name
, NVRAM_SIG_OS
,
308 part
->req_size
, part
->min_size
);
310 pr_info("nvram: No room to create %s partition, "
311 "deleting any obsolete OS partitions...\n",
313 nvram_remove_partition(NULL
, NVRAM_SIG_OS
,
314 nvram_os_partitions
);
315 p
= nvram_create_partition(part
->name
, NVRAM_SIG_OS
,
316 part
->req_size
, part
->min_size
);
321 pr_err("nvram: Failed to find or create %s"
322 " partition, err %d\n", part
->name
, (int)p
);
327 part
->size
= nvram_get_partition_size(p
) - sizeof(struct err_log_info
);
332 /* Derived from logfs_compress() */
333 static int nvram_compress(const void *in
, void *out
, size_t inlen
,
339 err
= zlib_deflateInit2(&stream
, COMPR_LEVEL
, Z_DEFLATED
, WINDOW_BITS
,
340 MEM_LEVEL
, Z_DEFAULT_STRATEGY
);
345 stream
.avail_in
= inlen
;
347 stream
.next_out
= out
;
348 stream
.avail_out
= outlen
;
349 stream
.total_out
= 0;
351 err
= zlib_deflate(&stream
, Z_FINISH
);
352 if (err
!= Z_STREAM_END
)
355 err
= zlib_deflateEnd(&stream
);
359 if (stream
.total_out
>= stream
.total_in
)
362 ret
= stream
.total_out
;
367 /* Compress the text from big_oops_buf into oops_buf. */
368 static int zip_oops(size_t text_len
)
370 struct oops_log_info
*oops_hdr
= (struct oops_log_info
*)oops_buf
;
371 int zipped_len
= nvram_compress(big_oops_buf
, oops_data
, text_len
,
373 if (zipped_len
< 0) {
374 pr_err("nvram: compression failed; returned %d\n", zipped_len
);
375 pr_err("nvram: logging uncompressed oops/panic report\n");
378 oops_hdr
->version
= cpu_to_be16(OOPS_HDR_VERSION
);
379 oops_hdr
->report_length
= cpu_to_be16(zipped_len
);
380 oops_hdr
->timestamp
= cpu_to_be64(ktime_get_real_seconds());
385 static int nvram_pstore_open(struct pstore_info
*psi
)
387 /* Reset the iterator to start reading partitions again */
393 * nvram_pstore_write - pstore write callback for nvram
394 * @type: Type of message logged
395 * @reason: reason behind dump (oops/panic)
396 * @id: identifier to indicate the write performed
397 * @part: pstore writes data to registered buffer in parts,
398 * part number will indicate the same.
399 * @count: Indicates oops count
400 * @compressed: Flag to indicate the log is compressed
401 * @size: number of bytes written to the registered buffer
402 * @psi: registered pstore_info structure
404 * Called by pstore_dump() when an oops or panic report is logged in the
406 * Returns 0 on successful write.
408 static int nvram_pstore_write(enum pstore_type_id type
,
409 enum kmsg_dump_reason reason
,
410 u64
*id
, unsigned int part
, int count
,
411 bool compressed
, size_t size
,
412 struct pstore_info
*psi
)
415 unsigned int err_type
= ERR_TYPE_KERNEL_PANIC
;
416 struct oops_log_info
*oops_hdr
= (struct oops_log_info
*) oops_buf
;
418 /* part 1 has the recent messages from printk buffer */
419 if (part
> 1 || (type
!= PSTORE_TYPE_DMESG
))
422 if (clobbering_unread_rtas_event())
425 oops_hdr
->version
= cpu_to_be16(OOPS_HDR_VERSION
);
426 oops_hdr
->report_length
= cpu_to_be16(size
);
427 oops_hdr
->timestamp
= cpu_to_be64(ktime_get_real_seconds());
430 err_type
= ERR_TYPE_KERNEL_PANIC_GZ
;
432 rc
= nvram_write_os_partition(&oops_log_partition
, oops_buf
,
433 (int) (sizeof(*oops_hdr
) + size
), err_type
, count
);
443 * Reads the oops/panic report, rtas, of-config and common partition.
444 * Returns the length of the data we read from each partition.
445 * Returns 0 if we've been called before.
447 static ssize_t
nvram_pstore_read(u64
*id
, enum pstore_type_id
*type
,
448 int *count
, struct timespec
*time
, char **buf
,
449 bool *compressed
, struct pstore_info
*psi
)
451 struct oops_log_info
*oops_hdr
;
452 unsigned int err_type
, id_no
, size
= 0;
453 struct nvram_os_partition
*part
= NULL
;
460 switch (nvram_type_ids
[read_type
]) {
461 case PSTORE_TYPE_DMESG
:
462 part
= &oops_log_partition
;
463 *type
= PSTORE_TYPE_DMESG
;
465 case PSTORE_TYPE_PPC_COMMON
:
467 part
= &common_partition
;
468 *type
= PSTORE_TYPE_PPC_COMMON
;
469 *id
= PSTORE_TYPE_PPC_COMMON
;
473 #ifdef CONFIG_PPC_PSERIES
474 case PSTORE_TYPE_PPC_RTAS
:
475 part
= &rtas_log_partition
;
476 *type
= PSTORE_TYPE_PPC_RTAS
;
477 time
->tv_sec
= last_rtas_event
;
480 case PSTORE_TYPE_PPC_OF
:
482 part
= &of_config_partition
;
483 *type
= PSTORE_TYPE_PPC_OF
;
484 *id
= PSTORE_TYPE_PPC_OF
;
489 #ifdef CONFIG_PPC_POWERNV
490 case PSTORE_TYPE_PPC_OPAL
:
492 part
= &skiboot_partition
;
493 *type
= PSTORE_TYPE_PPC_OPAL
;
494 *id
= PSTORE_TYPE_PPC_OPAL
;
503 if (!part
->os_partition
) {
504 p
= nvram_find_partition(part
->name
, sig
, &size
);
506 pr_err("nvram: Failed to find partition %s, "
507 "err %d\n", part
->name
, (int)p
);
514 buff
= kmalloc(part
->size
, GFP_KERNEL
);
519 if (nvram_read_partition(part
, buff
, part
->size
, &err_type
, &id_no
)) {
526 if (part
->os_partition
)
529 if (nvram_type_ids
[read_type
] == PSTORE_TYPE_DMESG
) {
530 size_t length
, hdr_size
;
532 oops_hdr
= (struct oops_log_info
*)buff
;
533 if (be16_to_cpu(oops_hdr
->version
) < OOPS_HDR_VERSION
) {
534 /* Old format oops header had 2-byte record size */
535 hdr_size
= sizeof(u16
);
536 length
= be16_to_cpu(oops_hdr
->version
);
540 hdr_size
= sizeof(*oops_hdr
);
541 length
= be16_to_cpu(oops_hdr
->report_length
);
542 time
->tv_sec
= be64_to_cpu(oops_hdr
->timestamp
);
545 *buf
= kmemdup(buff
+ hdr_size
, length
, GFP_KERNEL
);
550 if (err_type
== ERR_TYPE_KERNEL_PANIC_GZ
)
561 static struct pstore_info nvram_pstore_info
= {
562 .owner
= THIS_MODULE
,
564 .open
= nvram_pstore_open
,
565 .read
= nvram_pstore_read
,
566 .write
= nvram_pstore_write
,
569 static int nvram_pstore_init(void)
573 if (machine_is(pseries
)) {
574 nvram_type_ids
[2] = PSTORE_TYPE_PPC_RTAS
;
575 nvram_type_ids
[3] = PSTORE_TYPE_PPC_OF
;
577 nvram_type_ids
[2] = PSTORE_TYPE_PPC_OPAL
;
579 nvram_pstore_info
.buf
= oops_data
;
580 nvram_pstore_info
.bufsize
= oops_data_sz
;
582 spin_lock_init(&nvram_pstore_info
.buf_lock
);
584 rc
= pstore_register(&nvram_pstore_info
);
585 if (rc
&& (rc
!= -EPERM
))
586 /* Print error only when pstore.backend == nvram */
587 pr_err("nvram: pstore_register() failed, returned %d. "
588 "Defaults to kmsg_dump\n", rc
);
593 static int nvram_pstore_init(void)
599 void __init
nvram_init_oops_partition(int rtas_partition_exists
)
603 rc
= nvram_init_os_partition(&oops_log_partition
);
605 #ifdef CONFIG_PPC_PSERIES
606 if (!rtas_partition_exists
) {
607 pr_err("nvram: Failed to initialize oops partition!");
610 pr_notice("nvram: Using %s partition to log both"
611 " RTAS errors and oops/panic reports\n",
612 rtas_log_partition
.name
);
613 memcpy(&oops_log_partition
, &rtas_log_partition
,
614 sizeof(rtas_log_partition
));
616 pr_err("nvram: Failed to initialize oops partition!");
620 oops_buf
= kmalloc(oops_log_partition
.size
, GFP_KERNEL
);
622 pr_err("nvram: No memory for %s partition\n",
623 oops_log_partition
.name
);
626 oops_data
= oops_buf
+ sizeof(struct oops_log_info
);
627 oops_data_sz
= oops_log_partition
.size
- sizeof(struct oops_log_info
);
629 rc
= nvram_pstore_init();
635 * Figure compression (preceded by elimination of each line's <n>
636 * severity prefix) will reduce the oops/panic report to at most
637 * 45% of its original size.
639 big_oops_buf_sz
= (oops_data_sz
* 100) / 45;
640 big_oops_buf
= kmalloc(big_oops_buf_sz
, GFP_KERNEL
);
642 stream
.workspace
= kmalloc(zlib_deflate_workspacesize(
643 WINDOW_BITS
, MEM_LEVEL
), GFP_KERNEL
);
644 if (!stream
.workspace
) {
645 pr_err("nvram: No memory for compression workspace; "
646 "skipping compression of %s partition data\n",
647 oops_log_partition
.name
);
652 pr_err("No memory for uncompressed %s data; "
653 "skipping compression\n", oops_log_partition
.name
);
654 stream
.workspace
= NULL
;
657 rc
= kmsg_dump_register(&nvram_kmsg_dumper
);
659 pr_err("nvram: kmsg_dump_register() failed; returned %d\n", rc
);
662 kfree(stream
.workspace
);
667 * This is our kmsg_dump callback, called after an oops or panic report
668 * has been written to the printk buffer. We want to capture as much
669 * of the printk buffer as possible. First, capture as much as we can
670 * that we think will compress sufficiently to fit in the lnx,oops-log
671 * partition. If that's too much, go back and capture uncompressed text.
673 static void oops_to_nvram(struct kmsg_dumper
*dumper
,
674 enum kmsg_dump_reason reason
)
676 struct oops_log_info
*oops_hdr
= (struct oops_log_info
*)oops_buf
;
677 static unsigned int oops_count
= 0;
678 static bool panicking
= false;
679 static DEFINE_SPINLOCK(lock
);
682 unsigned int err_type
= ERR_TYPE_KERNEL_PANIC_GZ
;
686 case KMSG_DUMP_RESTART
:
688 case KMSG_DUMP_POWEROFF
:
689 /* These are almost always orderly shutdowns. */
693 case KMSG_DUMP_PANIC
:
696 case KMSG_DUMP_EMERG
:
698 /* Panic report already captured. */
702 pr_err("%s: ignoring unrecognized KMSG_DUMP_* reason %d\n",
703 __func__
, (int) reason
);
707 if (clobbering_unread_rtas_event())
710 if (!spin_trylock_irqsave(&lock
, flags
))
714 kmsg_dump_get_buffer(dumper
, false,
715 big_oops_buf
, big_oops_buf_sz
, &text_len
);
716 rc
= zip_oops(text_len
);
719 kmsg_dump_rewind(dumper
);
720 kmsg_dump_get_buffer(dumper
, false,
721 oops_data
, oops_data_sz
, &text_len
);
722 err_type
= ERR_TYPE_KERNEL_PANIC
;
723 oops_hdr
->version
= cpu_to_be16(OOPS_HDR_VERSION
);
724 oops_hdr
->report_length
= cpu_to_be16(text_len
);
725 oops_hdr
->timestamp
= cpu_to_be64(ktime_get_real_seconds());
728 (void) nvram_write_os_partition(&oops_log_partition
, oops_buf
,
729 (int) (sizeof(*oops_hdr
) + text_len
), err_type
,
732 spin_unlock_irqrestore(&lock
, flags
);
735 static loff_t
dev_nvram_llseek(struct file
*file
, loff_t offset
, int origin
)
737 if (ppc_md
.nvram_size
== NULL
)
739 return generic_file_llseek_size(file
, offset
, origin
, MAX_LFS_FILESIZE
,
740 ppc_md
.nvram_size());
744 static ssize_t
dev_nvram_read(struct file
*file
, char __user
*buf
,
745 size_t count
, loff_t
*ppos
)
751 if (!ppc_md
.nvram_size
) {
756 size
= ppc_md
.nvram_size();
767 count
= min_t(size_t, count
, size
- *ppos
);
768 count
= min(count
, PAGE_SIZE
);
770 tmp
= kmalloc(count
, GFP_KERNEL
);
776 ret
= ppc_md
.nvram_read(tmp
, count
, ppos
);
780 if (copy_to_user(buf
, tmp
, ret
))
789 static ssize_t
dev_nvram_write(struct file
*file
, const char __user
*buf
,
790 size_t count
, loff_t
*ppos
)
797 if (!ppc_md
.nvram_size
)
801 size
= ppc_md
.nvram_size();
802 if (*ppos
>= size
|| size
< 0)
805 count
= min_t(size_t, count
, size
- *ppos
);
806 count
= min(count
, PAGE_SIZE
);
809 tmp
= kmalloc(count
, GFP_KERNEL
);
814 if (copy_from_user(tmp
, buf
, count
))
817 ret
= ppc_md
.nvram_write(tmp
, count
, ppos
);
825 static long dev_nvram_ioctl(struct file
*file
, unsigned int cmd
,
829 #ifdef CONFIG_PPC_PMAC
830 case OBSOLETE_PMAC_NVRAM_GET_OFFSET
:
831 printk(KERN_WARNING
"nvram: Using obsolete PMAC_NVRAM_GET_OFFSET ioctl\n");
832 case IOC_NVRAM_GET_OFFSET
: {
835 if (!machine_is(powermac
))
837 if (copy_from_user(&part
, (void __user
*)arg
, sizeof(part
)) != 0)
839 if (part
< pmac_nvram_OF
|| part
> pmac_nvram_NR
)
841 offset
= pmac_get_partition(part
);
844 if (copy_to_user((void __user
*)arg
, &offset
, sizeof(offset
)) != 0)
848 #endif /* CONFIG_PPC_PMAC */
854 const struct file_operations nvram_fops
= {
855 .owner
= THIS_MODULE
,
856 .llseek
= dev_nvram_llseek
,
857 .read
= dev_nvram_read
,
858 .write
= dev_nvram_write
,
859 .unlocked_ioctl
= dev_nvram_ioctl
,
862 static struct miscdevice nvram_dev
= {
870 static void __init
nvram_print_partitions(char * label
)
872 struct nvram_partition
* tmp_part
;
874 printk(KERN_WARNING
"--------%s---------\n", label
);
875 printk(KERN_WARNING
"indx\t\tsig\tchks\tlen\tname\n");
876 list_for_each_entry(tmp_part
, &nvram_partitions
, partition
) {
877 printk(KERN_WARNING
"%4d \t%02x\t%02x\t%d\t%12.12s\n",
878 tmp_part
->index
, tmp_part
->header
.signature
,
879 tmp_part
->header
.checksum
, tmp_part
->header
.length
,
880 tmp_part
->header
.name
);
886 static int __init
nvram_write_header(struct nvram_partition
* part
)
890 struct nvram_header phead
;
892 memcpy(&phead
, &part
->header
, NVRAM_HEADER_LEN
);
893 phead
.length
= cpu_to_be16(phead
.length
);
895 tmp_index
= part
->index
;
896 rc
= ppc_md
.nvram_write((char *)&phead
, NVRAM_HEADER_LEN
, &tmp_index
);
902 static unsigned char __init
nvram_checksum(struct nvram_header
*p
)
904 unsigned int c_sum
, c_sum2
;
905 unsigned short *sp
= (unsigned short *)p
->name
; /* assume 6 shorts */
906 c_sum
= p
->signature
+ p
->length
+ sp
[0] + sp
[1] + sp
[2] + sp
[3] + sp
[4] + sp
[5];
908 /* The sum may have spilled into the 3rd byte. Fold it back. */
909 c_sum
= ((c_sum
& 0xffff) + (c_sum
>> 16)) & 0xffff;
910 /* The sum cannot exceed 2 bytes. Fold it into a checksum */
911 c_sum2
= (c_sum
>> 8) + (c_sum
<< 8);
912 c_sum
= ((c_sum
+ c_sum2
) >> 8) & 0xff;
917 * Per the criteria passed via nvram_remove_partition(), should this
918 * partition be removed? 1=remove, 0=keep
920 static int nvram_can_remove_partition(struct nvram_partition
*part
,
921 const char *name
, int sig
, const char *exceptions
[])
923 if (part
->header
.signature
!= sig
)
926 if (strncmp(name
, part
->header
.name
, 12))
928 } else if (exceptions
) {
930 for (except
= exceptions
; *except
; except
++) {
931 if (!strncmp(*except
, part
->header
.name
, 12))
939 * nvram_remove_partition - Remove one or more partitions in nvram
940 * @name: name of the partition to remove, or NULL for a
941 * signature only match
942 * @sig: signature of the partition(s) to remove
943 * @exceptions: When removing all partitions with a matching signature,
947 int __init
nvram_remove_partition(const char *name
, int sig
,
948 const char *exceptions
[])
950 struct nvram_partition
*part
, *prev
, *tmp
;
953 list_for_each_entry(part
, &nvram_partitions
, partition
) {
954 if (!nvram_can_remove_partition(part
, name
, sig
, exceptions
))
957 /* Make partition a free partition */
958 part
->header
.signature
= NVRAM_SIG_FREE
;
959 strncpy(part
->header
.name
, "wwwwwwwwwwww", 12);
960 part
->header
.checksum
= nvram_checksum(&part
->header
);
961 rc
= nvram_write_header(part
);
963 printk(KERN_ERR
"nvram_remove_partition: nvram_write failed (%d)\n", rc
);
968 /* Merge contiguous ones */
970 list_for_each_entry_safe(part
, tmp
, &nvram_partitions
, partition
) {
971 if (part
->header
.signature
!= NVRAM_SIG_FREE
) {
976 prev
->header
.length
+= part
->header
.length
;
977 prev
->header
.checksum
= nvram_checksum(&part
->header
);
978 rc
= nvram_write_header(part
);
980 printk(KERN_ERR
"nvram_remove_partition: nvram_write failed (%d)\n", rc
);
983 list_del(&part
->partition
);
993 * nvram_create_partition - Create a partition in nvram
994 * @name: name of the partition to create
995 * @sig: signature of the partition to create
996 * @req_size: size of data to allocate in bytes
997 * @min_size: minimum acceptable size (0 means req_size)
999 * Returns a negative error code or a positive nvram index
1000 * of the beginning of the data area of the newly created
1001 * partition. If you provided a min_size smaller than req_size
1002 * you need to query for the actual size yourself after the
1003 * call using nvram_partition_get_size().
1005 loff_t __init
nvram_create_partition(const char *name
, int sig
,
1006 int req_size
, int min_size
)
1008 struct nvram_partition
*part
;
1009 struct nvram_partition
*new_part
;
1010 struct nvram_partition
*free_part
= NULL
;
1011 static char nv_init_vals
[16];
1016 /* Convert sizes from bytes to blocks */
1017 req_size
= _ALIGN_UP(req_size
, NVRAM_BLOCK_LEN
) / NVRAM_BLOCK_LEN
;
1018 min_size
= _ALIGN_UP(min_size
, NVRAM_BLOCK_LEN
) / NVRAM_BLOCK_LEN
;
1020 /* If no minimum size specified, make it the same as the
1024 min_size
= req_size
;
1025 if (min_size
> req_size
)
1028 /* Now add one block to each for the header */
1032 /* Find a free partition that will give us the maximum needed size
1033 If can't find one that will give us the minimum size needed */
1034 list_for_each_entry(part
, &nvram_partitions
, partition
) {
1035 if (part
->header
.signature
!= NVRAM_SIG_FREE
)
1038 if (part
->header
.length
>= req_size
) {
1043 if (part
->header
.length
> size
&&
1044 part
->header
.length
>= min_size
) {
1045 size
= part
->header
.length
;
1052 /* Create our OS partition */
1053 new_part
= kmalloc(sizeof(*new_part
), GFP_KERNEL
);
1055 pr_err("%s: kmalloc failed\n", __func__
);
1059 new_part
->index
= free_part
->index
;
1060 new_part
->header
.signature
= sig
;
1061 new_part
->header
.length
= size
;
1062 strncpy(new_part
->header
.name
, name
, 12);
1063 new_part
->header
.checksum
= nvram_checksum(&new_part
->header
);
1065 rc
= nvram_write_header(new_part
);
1067 pr_err("%s: nvram_write_header failed (%d)\n", __func__
, rc
);
1071 list_add_tail(&new_part
->partition
, &free_part
->partition
);
1073 /* Adjust or remove the partition we stole the space from */
1074 if (free_part
->header
.length
> size
) {
1075 free_part
->index
+= size
* NVRAM_BLOCK_LEN
;
1076 free_part
->header
.length
-= size
;
1077 free_part
->header
.checksum
= nvram_checksum(&free_part
->header
);
1078 rc
= nvram_write_header(free_part
);
1080 pr_err("%s: nvram_write_header failed (%d)\n",
1085 list_del(&free_part
->partition
);
1089 /* Clear the new partition */
1090 for (tmp_index
= new_part
->index
+ NVRAM_HEADER_LEN
;
1091 tmp_index
< ((size
- 1) * NVRAM_BLOCK_LEN
);
1092 tmp_index
+= NVRAM_BLOCK_LEN
) {
1093 rc
= ppc_md
.nvram_write(nv_init_vals
, NVRAM_BLOCK_LEN
, &tmp_index
);
1095 pr_err("%s: nvram_write failed (%d)\n",
1101 return new_part
->index
+ NVRAM_HEADER_LEN
;
1105 * nvram_get_partition_size - Get the data size of an nvram partition
1106 * @data_index: This is the offset of the start of the data of
1107 * the partition. The same value that is returned by
1108 * nvram_create_partition().
1110 int nvram_get_partition_size(loff_t data_index
)
1112 struct nvram_partition
*part
;
1114 list_for_each_entry(part
, &nvram_partitions
, partition
) {
1115 if (part
->index
+ NVRAM_HEADER_LEN
== data_index
)
1116 return (part
->header
.length
- 1) * NVRAM_BLOCK_LEN
;
1123 * nvram_find_partition - Find an nvram partition by signature and name
1124 * @name: Name of the partition or NULL for any name
1125 * @sig: Signature to test against
1126 * @out_size: if non-NULL, returns the size of the data part of the partition
1128 loff_t
nvram_find_partition(const char *name
, int sig
, int *out_size
)
1130 struct nvram_partition
*p
;
1132 list_for_each_entry(p
, &nvram_partitions
, partition
) {
1133 if (p
->header
.signature
== sig
&&
1134 (!name
|| !strncmp(p
->header
.name
, name
, 12))) {
1136 *out_size
= (p
->header
.length
- 1) *
1138 return p
->index
+ NVRAM_HEADER_LEN
;
1144 int __init
nvram_scan_partitions(void)
1146 loff_t cur_index
= 0;
1147 struct nvram_header phead
;
1148 struct nvram_partition
* tmp_part
;
1149 unsigned char c_sum
;
1154 if (ppc_md
.nvram_size
== NULL
|| ppc_md
.nvram_size() <= 0)
1156 total_size
= ppc_md
.nvram_size();
1158 header
= kmalloc(NVRAM_HEADER_LEN
, GFP_KERNEL
);
1160 printk(KERN_ERR
"nvram_scan_partitions: Failed kmalloc\n");
1164 while (cur_index
< total_size
) {
1166 err
= ppc_md
.nvram_read(header
, NVRAM_HEADER_LEN
, &cur_index
);
1167 if (err
!= NVRAM_HEADER_LEN
) {
1168 printk(KERN_ERR
"nvram_scan_partitions: Error parsing "
1169 "nvram partitions\n");
1173 cur_index
-= NVRAM_HEADER_LEN
; /* nvram_read will advance us */
1175 memcpy(&phead
, header
, NVRAM_HEADER_LEN
);
1177 phead
.length
= be16_to_cpu(phead
.length
);
1180 c_sum
= nvram_checksum(&phead
);
1181 if (c_sum
!= phead
.checksum
) {
1182 printk(KERN_WARNING
"WARNING: nvram partition checksum"
1183 " was %02x, should be %02x!\n",
1184 phead
.checksum
, c_sum
);
1185 printk(KERN_WARNING
"Terminating nvram partition scan\n");
1188 if (!phead
.length
) {
1189 printk(KERN_WARNING
"WARNING: nvram corruption "
1190 "detected: 0-length partition\n");
1193 tmp_part
= kmalloc(sizeof(struct nvram_partition
), GFP_KERNEL
);
1196 printk(KERN_ERR
"nvram_scan_partitions: kmalloc failed\n");
1200 memcpy(&tmp_part
->header
, &phead
, NVRAM_HEADER_LEN
);
1201 tmp_part
->index
= cur_index
;
1202 list_add_tail(&tmp_part
->partition
, &nvram_partitions
);
1204 cur_index
+= phead
.length
* NVRAM_BLOCK_LEN
;
1209 nvram_print_partitions("NVRAM Partitions");
1217 static int __init
nvram_init(void)
1221 BUILD_BUG_ON(NVRAM_BLOCK_LEN
!= 16);
1223 if (ppc_md
.nvram_size
== NULL
|| ppc_md
.nvram_size() <= 0)
1226 rc
= misc_register(&nvram_dev
);
1228 printk(KERN_ERR
"nvram_init: failed to register device\n");
1235 static void __exit
nvram_cleanup(void)
1237 misc_deregister( &nvram_dev
);
1240 module_init(nvram_init
);
1241 module_exit(nvram_cleanup
);
1242 MODULE_LICENSE("GPL");