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_PSERIES
131 static struct nvram_os_partition of_config_partition = {
134 .os_partition = false
138 static struct nvram_os_partition common_partition = {
141 .os_partition = false
144 static enum pstore_type_id nvram_type_ids[] = {
146 PSTORE_TYPE_PPC_COMMON,
151 static int read_type;
154 /* nvram_write_os_partition
156 * We need to buffer the error logs into nvram to ensure that we have
157 * the failure information to decode. If we have a severe error there
158 * is no way to guarantee that the OS or the machine is in a state to
159 * get back to user land and write the error to disk. For example if
160 * the SCSI device driver causes a Machine Check by writing to a bad
161 * IO address, there is no way of guaranteeing that the device driver
162 * is in any state that is would also be able to write the error data
163 * captured to disk, thus we buffer it in NVRAM for analysis on the
166 * In NVRAM the partition containing the error log buffer will looks like:
168 * +-----------+----------+--------+------------+------------------+
169 * | signature | checksum | length | name | data |
170 * |0 |1 |2 3|4 15|16 length-1|
171 * +-----------+----------+--------+------------+------------------+
173 * The 'data' section would look like (in bytes):
174 * +--------------+------------+-----------------------------------+
175 * | event_logged | sequence # | error log |
176 * |0 3|4 7|8 error_log_size-1|
177 * +--------------+------------+-----------------------------------+
179 * event_logged: 0 if event has not been logged to syslog, 1 if it has
180 * sequence #: The unique sequence # for each event. (until it wraps)
181 * error log: The error log from event_scan
183 int nvram_write_os_partition(struct nvram_os_partition *part,
184 char *buff, int length,
185 unsigned int err_type,
186 unsigned int error_log_cnt)
190 struct err_log_info info;
192 if (part->index == -1)
195 if (length > part->size)
198 info.error_type = cpu_to_be32(err_type);
199 info.seq_num = cpu_to_be32(error_log_cnt);
201 tmp_index = part->index;
203 rc = ppc_md.nvram_write((char *)&info, sizeof(struct err_log_info),
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,
241 sizeof(struct err_log_info),
244 pr_err("%s: Failed nvram_read (%d)\n", __func__, rc);
249 rc = ppc_md.nvram_read(buff, length, &tmp_index);
251 pr_err("%s: Failed nvram_read (%d)\n", __func__, rc);
255 if (part->os_partition) {
256 *error_log_cnt = be32_to_cpu(info.seq_num);
257 *err_type = be32_to_cpu(info.error_type);
263 /* nvram_init_os_partition
265 * This sets up a partition with an "OS" signature.
267 * The general strategy is the following:
268 * 1.) If a partition with the indicated name already exists...
269 * - If it's large enough, use it.
270 * - Otherwise, recycle it and keep going.
271 * 2.) Search for a free partition that is large enough.
272 * 3.) If there's not a free partition large enough, recycle any obsolete
273 * OS partitions and try again.
274 * 4.) Will first try getting a chunk that will satisfy the requested size.
275 * 5.) If a chunk of the requested size cannot be allocated, then try finding
276 * a chunk that will satisfy the minum needed.
278 * Returns 0 on success, else -1.
280 int __init nvram_init_os_partition(struct nvram_os_partition *part)
286 p = nvram_find_partition(part->name, NVRAM_SIG_OS, &size);
288 /* Found one but too small, remove it */
289 if (p && size < part->min_size) {
290 pr_info("nvram: Found too small %s partition,"
291 " removing it...\n", part->name);
292 nvram_remove_partition(part->name, NVRAM_SIG_OS, NULL);
296 /* Create one if we didn't find */
298 p = nvram_create_partition(part->name, NVRAM_SIG_OS,
299 part->req_size, part->min_size);
301 pr_info("nvram: No room to create %s partition, "
302 "deleting any obsolete OS partitions...\n",
304 nvram_remove_partition(NULL, NVRAM_SIG_OS,
305 nvram_os_partitions);
306 p = nvram_create_partition(part->name, NVRAM_SIG_OS,
307 part->req_size, part->min_size);
312 pr_err("nvram: Failed to find or create %s"
313 " partition, err %d\n", part->name, (int)p);
318 part->size = nvram_get_partition_size(p) - sizeof(struct err_log_info);
323 /* Derived from logfs_compress() */
324 static int nvram_compress(const void *in, void *out, size_t inlen,
330 err = zlib_deflateInit2(&stream, COMPR_LEVEL, Z_DEFLATED, WINDOW_BITS,
331 MEM_LEVEL, Z_DEFAULT_STRATEGY);
336 stream.avail_in = inlen;
338 stream.next_out = out;
339 stream.avail_out = outlen;
340 stream.total_out = 0;
342 err = zlib_deflate(&stream, Z_FINISH);
343 if (err != Z_STREAM_END)
346 err = zlib_deflateEnd(&stream);
350 if (stream.total_out >= stream.total_in)
353 ret = stream.total_out;
358 /* Compress the text from big_oops_buf into oops_buf. */
359 static int zip_oops(size_t text_len)
361 struct oops_log_info *oops_hdr = (struct oops_log_info *)oops_buf;
362 int zipped_len = nvram_compress(big_oops_buf, oops_data, text_len,
364 if (zipped_len < 0) {
365 pr_err("nvram: compression failed; returned %d\n", zipped_len);
366 pr_err("nvram: logging uncompressed oops/panic report\n");
369 oops_hdr->version = cpu_to_be16(OOPS_HDR_VERSION);
370 oops_hdr->report_length = cpu_to_be16(zipped_len);
371 oops_hdr->timestamp = cpu_to_be64(get_seconds());
376 static int nvram_pstore_open(struct pstore_info *psi)
378 /* Reset the iterator to start reading partitions again */
384 * nvram_pstore_write - pstore write callback for nvram
385 * @type: Type of message logged
386 * @reason: reason behind dump (oops/panic)
387 * @id: identifier to indicate the write performed
388 * @part: pstore writes data to registered buffer in parts,
389 * part number will indicate the same.
390 * @count: Indicates oops count
391 * @compressed: Flag to indicate the log is compressed
392 * @size: number of bytes written to the registered buffer
393 * @psi: registered pstore_info structure
395 * Called by pstore_dump() when an oops or panic report is logged in the
397 * Returns 0 on successful write.
399 static int nvram_pstore_write(enum pstore_type_id type,
400 enum kmsg_dump_reason reason,
401 u64 *id, unsigned int part, int count,
402 bool compressed, size_t size,
403 struct pstore_info *psi)
406 unsigned int err_type = ERR_TYPE_KERNEL_PANIC;
407 struct oops_log_info *oops_hdr = (struct oops_log_info *) oops_buf;
409 /* part 1 has the recent messages from printk buffer */
410 if (part > 1 || (type != PSTORE_TYPE_DMESG))
413 if (clobbering_unread_rtas_event())
416 oops_hdr->version = cpu_to_be16(OOPS_HDR_VERSION);
417 oops_hdr->report_length = cpu_to_be16(size);
418 oops_hdr->timestamp = cpu_to_be64(get_seconds());
421 err_type = ERR_TYPE_KERNEL_PANIC_GZ;
423 rc = nvram_write_os_partition(&oops_log_partition, oops_buf,
424 (int) (sizeof(*oops_hdr) + size), err_type, count);
434 * Reads the oops/panic report, rtas, of-config and common partition.
435 * Returns the length of the data we read from each partition.
436 * Returns 0 if we've been called before.
438 static ssize_t nvram_pstore_read(u64 *id, enum pstore_type_id *type,
439 int *count, struct timespec *time, char **buf,
440 bool *compressed, struct pstore_info *psi)
442 struct oops_log_info *oops_hdr;
443 unsigned int err_type, id_no, size = 0;
444 struct nvram_os_partition *part = NULL;
451 switch (nvram_type_ids[read_type]) {
452 case PSTORE_TYPE_DMESG:
453 part = &oops_log_partition;
454 *type = PSTORE_TYPE_DMESG;
456 case PSTORE_TYPE_PPC_COMMON:
458 part = &common_partition;
459 *type = PSTORE_TYPE_PPC_COMMON;
460 *id = PSTORE_TYPE_PPC_COMMON;
464 #ifdef CONFIG_PPC_PSERIES
465 case PSTORE_TYPE_PPC_RTAS:
466 part = &rtas_log_partition;
467 *type = PSTORE_TYPE_PPC_RTAS;
468 time->tv_sec = last_rtas_event;
471 case PSTORE_TYPE_PPC_OF:
473 part = &of_config_partition;
474 *type = PSTORE_TYPE_PPC_OF;
475 *id = PSTORE_TYPE_PPC_OF;
484 if (!part->os_partition) {
485 p = nvram_find_partition(part->name, sig, &size);
487 pr_err("nvram: Failed to find partition %s, "
488 "err %d\n", part->name, (int)p);
495 buff = kmalloc(part->size, GFP_KERNEL);
500 if (nvram_read_partition(part, buff, part->size, &err_type, &id_no)) {
507 if (part->os_partition)
510 if (nvram_type_ids[read_type] == PSTORE_TYPE_DMESG) {
511 size_t length, hdr_size;
513 oops_hdr = (struct oops_log_info *)buff;
514 if (be16_to_cpu(oops_hdr->version) < OOPS_HDR_VERSION) {
515 /* Old format oops header had 2-byte record size */
516 hdr_size = sizeof(u16);
517 length = be16_to_cpu(oops_hdr->version);
521 hdr_size = sizeof(*oops_hdr);
522 length = be16_to_cpu(oops_hdr->report_length);
523 time->tv_sec = be64_to_cpu(oops_hdr->timestamp);
526 *buf = kmalloc(length, GFP_KERNEL);
529 memcpy(*buf, buff + hdr_size, length);
532 if (err_type == ERR_TYPE_KERNEL_PANIC_GZ)
543 static struct pstore_info nvram_pstore_info = {
544 .owner = THIS_MODULE,
546 .open = nvram_pstore_open,
547 .read = nvram_pstore_read,
548 .write = nvram_pstore_write,
551 static int nvram_pstore_init(void)
555 nvram_type_ids[2] = PSTORE_TYPE_PPC_RTAS;
556 nvram_type_ids[3] = PSTORE_TYPE_PPC_OF;
558 nvram_pstore_info.buf = oops_data;
559 nvram_pstore_info.bufsize = oops_data_sz;
561 spin_lock_init(&nvram_pstore_info.buf_lock);
563 rc = pstore_register(&nvram_pstore_info);
565 pr_err("nvram: pstore_register() failed, defaults to "
566 "kmsg_dump; returned %d\n", rc);
571 static int nvram_pstore_init(void)
577 void __init nvram_init_oops_partition(int rtas_partition_exists)
581 rc = nvram_init_os_partition(&oops_log_partition);
583 #ifdef CONFIG_PPC_PSERIES
584 if (!rtas_partition_exists) {
585 pr_err("nvram: Failed to initialize oops partition!");
588 pr_notice("nvram: Using %s partition to log both"
589 " RTAS errors and oops/panic reports\n",
590 rtas_log_partition.name);
591 memcpy(&oops_log_partition, &rtas_log_partition,
592 sizeof(rtas_log_partition));
594 pr_err("nvram: Failed to initialize oops partition!");
598 oops_buf = kmalloc(oops_log_partition.size, GFP_KERNEL);
600 pr_err("nvram: No memory for %s partition\n",
601 oops_log_partition.name);
604 oops_data = oops_buf + sizeof(struct oops_log_info);
605 oops_data_sz = oops_log_partition.size - sizeof(struct oops_log_info);
607 rc = nvram_pstore_init();
613 * Figure compression (preceded by elimination of each line's <n>
614 * severity prefix) will reduce the oops/panic report to at most
615 * 45% of its original size.
617 big_oops_buf_sz = (oops_data_sz * 100) / 45;
618 big_oops_buf = kmalloc(big_oops_buf_sz, GFP_KERNEL);
620 stream.workspace = kmalloc(zlib_deflate_workspacesize(
621 WINDOW_BITS, MEM_LEVEL), GFP_KERNEL);
622 if (!stream.workspace) {
623 pr_err("nvram: No memory for compression workspace; "
624 "skipping compression of %s partition data\n",
625 oops_log_partition.name);
630 pr_err("No memory for uncompressed %s data; "
631 "skipping compression\n", oops_log_partition.name);
632 stream.workspace = NULL;
635 rc = kmsg_dump_register(&nvram_kmsg_dumper);
637 pr_err("nvram: kmsg_dump_register() failed; returned %d\n", rc);
640 kfree(stream.workspace);
645 * This is our kmsg_dump callback, called after an oops or panic report
646 * has been written to the printk buffer. We want to capture as much
647 * of the printk buffer as possible. First, capture as much as we can
648 * that we think will compress sufficiently to fit in the lnx,oops-log
649 * partition. If that's too much, go back and capture uncompressed text.
651 static void oops_to_nvram(struct kmsg_dumper *dumper,
652 enum kmsg_dump_reason reason)
654 struct oops_log_info *oops_hdr = (struct oops_log_info *)oops_buf;
655 static unsigned int oops_count = 0;
656 static bool panicking = false;
657 static DEFINE_SPINLOCK(lock);
660 unsigned int err_type = ERR_TYPE_KERNEL_PANIC_GZ;
664 case KMSG_DUMP_RESTART:
666 case KMSG_DUMP_POWEROFF:
667 /* These are almost always orderly shutdowns. */
671 case KMSG_DUMP_PANIC:
674 case KMSG_DUMP_EMERG:
676 /* Panic report already captured. */
680 pr_err("%s: ignoring unrecognized KMSG_DUMP_* reason %d\n",
681 __func__, (int) reason);
685 if (clobbering_unread_rtas_event())
688 if (!spin_trylock_irqsave(&lock, flags))
692 kmsg_dump_get_buffer(dumper, false,
693 big_oops_buf, big_oops_buf_sz, &text_len);
694 rc = zip_oops(text_len);
697 kmsg_dump_rewind(dumper);
698 kmsg_dump_get_buffer(dumper, false,
699 oops_data, oops_data_sz, &text_len);
700 err_type = ERR_TYPE_KERNEL_PANIC;
701 oops_hdr->version = cpu_to_be16(OOPS_HDR_VERSION);
702 oops_hdr->report_length = cpu_to_be16(text_len);
703 oops_hdr->timestamp = cpu_to_be64(get_seconds());
706 (void) nvram_write_os_partition(&oops_log_partition, oops_buf,
707 (int) (sizeof(*oops_hdr) + text_len), err_type,
710 spin_unlock_irqrestore(&lock, flags);
713 static loff_t dev_nvram_llseek(struct file *file, loff_t offset, int origin)
717 if (ppc_md.nvram_size == NULL)
719 size = ppc_md.nvram_size();
723 offset += file->f_pos;
731 file->f_pos = offset;
736 static ssize_t dev_nvram_read(struct file *file, char __user *buf,
737 size_t count, loff_t *ppos)
743 if (!ppc_md.nvram_size) {
748 size = ppc_md.nvram_size();
759 count = min_t(size_t, count, size - *ppos);
760 count = min(count, PAGE_SIZE);
762 tmp = kmalloc(count, GFP_KERNEL);
768 ret = ppc_md.nvram_read(tmp, count, ppos);
772 if (copy_to_user(buf, tmp, ret))
781 static ssize_t dev_nvram_write(struct file *file, const char __user *buf,
782 size_t count, loff_t *ppos)
789 if (!ppc_md.nvram_size)
793 size = ppc_md.nvram_size();
794 if (*ppos >= size || size < 0)
797 count = min_t(size_t, count, size - *ppos);
798 count = min(count, PAGE_SIZE);
801 tmp = kmalloc(count, GFP_KERNEL);
806 if (copy_from_user(tmp, buf, count))
809 ret = ppc_md.nvram_write(tmp, count, ppos);
817 static long dev_nvram_ioctl(struct file *file, unsigned int cmd,
821 #ifdef CONFIG_PPC_PMAC
822 case OBSOLETE_PMAC_NVRAM_GET_OFFSET:
823 printk(KERN_WARNING "nvram: Using obsolete PMAC_NVRAM_GET_OFFSET ioctl\n");
824 case IOC_NVRAM_GET_OFFSET: {
827 if (!machine_is(powermac))
829 if (copy_from_user(&part, (void __user*)arg, sizeof(part)) != 0)
831 if (part < pmac_nvram_OF || part > pmac_nvram_NR)
833 offset = pmac_get_partition(part);
836 if (copy_to_user((void __user*)arg, &offset, sizeof(offset)) != 0)
840 #endif /* CONFIG_PPC_PMAC */
846 const struct file_operations nvram_fops = {
847 .owner = THIS_MODULE,
848 .llseek = dev_nvram_llseek,
849 .read = dev_nvram_read,
850 .write = dev_nvram_write,
851 .unlocked_ioctl = dev_nvram_ioctl,
854 static struct miscdevice nvram_dev = {
862 static void __init nvram_print_partitions(char * label)
864 struct nvram_partition * tmp_part;
866 printk(KERN_WARNING "--------%s---------\n", label);
867 printk(KERN_WARNING "indx\t\tsig\tchks\tlen\tname\n");
868 list_for_each_entry(tmp_part, &nvram_partitions, partition) {
869 printk(KERN_WARNING "%4d \t%02x\t%02x\t%d\t%12.12s\n",
870 tmp_part->index, tmp_part->header.signature,
871 tmp_part->header.checksum, tmp_part->header.length,
872 tmp_part->header.name);
878 static int __init nvram_write_header(struct nvram_partition * part)
882 struct nvram_header phead;
884 memcpy(&phead, &part->header, NVRAM_HEADER_LEN);
885 phead.length = cpu_to_be16(phead.length);
887 tmp_index = part->index;
888 rc = ppc_md.nvram_write((char *)&phead, NVRAM_HEADER_LEN, &tmp_index);
894 static unsigned char __init nvram_checksum(struct nvram_header *p)
896 unsigned int c_sum, c_sum2;
897 unsigned short *sp = (unsigned short *)p->name; /* assume 6 shorts */
898 c_sum = p->signature + p->length + sp[0] + sp[1] + sp[2] + sp[3] + sp[4] + sp[5];
900 /* The sum may have spilled into the 3rd byte. Fold it back. */
901 c_sum = ((c_sum & 0xffff) + (c_sum >> 16)) & 0xffff;
902 /* The sum cannot exceed 2 bytes. Fold it into a checksum */
903 c_sum2 = (c_sum >> 8) + (c_sum << 8);
904 c_sum = ((c_sum + c_sum2) >> 8) & 0xff;
909 * Per the criteria passed via nvram_remove_partition(), should this
910 * partition be removed? 1=remove, 0=keep
912 static int nvram_can_remove_partition(struct nvram_partition *part,
913 const char *name, int sig, const char *exceptions[])
915 if (part->header.signature != sig)
918 if (strncmp(name, part->header.name, 12))
920 } else if (exceptions) {
922 for (except = exceptions; *except; except++) {
923 if (!strncmp(*except, part->header.name, 12))
931 * nvram_remove_partition - Remove one or more partitions in nvram
932 * @name: name of the partition to remove, or NULL for a
933 * signature only match
934 * @sig: signature of the partition(s) to remove
935 * @exceptions: When removing all partitions with a matching signature,
939 int __init nvram_remove_partition(const char *name, int sig,
940 const char *exceptions[])
942 struct nvram_partition *part, *prev, *tmp;
945 list_for_each_entry(part, &nvram_partitions, partition) {
946 if (!nvram_can_remove_partition(part, name, sig, exceptions))
949 /* Make partition a free partition */
950 part->header.signature = NVRAM_SIG_FREE;
951 strncpy(part->header.name, "wwwwwwwwwwww", 12);
952 part->header.checksum = nvram_checksum(&part->header);
953 rc = nvram_write_header(part);
955 printk(KERN_ERR "nvram_remove_partition: nvram_write failed (%d)\n", rc);
960 /* Merge contiguous ones */
962 list_for_each_entry_safe(part, tmp, &nvram_partitions, partition) {
963 if (part->header.signature != NVRAM_SIG_FREE) {
968 prev->header.length += part->header.length;
969 prev->header.checksum = nvram_checksum(&part->header);
970 rc = nvram_write_header(part);
972 printk(KERN_ERR "nvram_remove_partition: nvram_write failed (%d)\n", rc);
975 list_del(&part->partition);
985 * nvram_create_partition - Create a partition in nvram
986 * @name: name of the partition to create
987 * @sig: signature of the partition to create
988 * @req_size: size of data to allocate in bytes
989 * @min_size: minimum acceptable size (0 means req_size)
991 * Returns a negative error code or a positive nvram index
992 * of the beginning of the data area of the newly created
993 * partition. If you provided a min_size smaller than req_size
994 * you need to query for the actual size yourself after the
995 * call using nvram_partition_get_size().
997 loff_t __init nvram_create_partition(const char *name, int sig,
998 int req_size, int min_size)
1000 struct nvram_partition *part;
1001 struct nvram_partition *new_part;
1002 struct nvram_partition *free_part = NULL;
1003 static char nv_init_vals[16];
1008 /* Convert sizes from bytes to blocks */
1009 req_size = _ALIGN_UP(req_size, NVRAM_BLOCK_LEN) / NVRAM_BLOCK_LEN;
1010 min_size = _ALIGN_UP(min_size, NVRAM_BLOCK_LEN) / NVRAM_BLOCK_LEN;
1012 /* If no minimum size specified, make it the same as the
1016 min_size = req_size;
1017 if (min_size > req_size)
1020 /* Now add one block to each for the header */
1024 /* Find a free partition that will give us the maximum needed size
1025 If can't find one that will give us the minimum size needed */
1026 list_for_each_entry(part, &nvram_partitions, partition) {
1027 if (part->header.signature != NVRAM_SIG_FREE)
1030 if (part->header.length >= req_size) {
1035 if (part->header.length > size &&
1036 part->header.length >= min_size) {
1037 size = part->header.length;
1044 /* Create our OS partition */
1045 new_part = kmalloc(sizeof(*new_part), GFP_KERNEL);
1047 pr_err("nvram_create_os_partition: kmalloc failed\n");
1051 new_part->index = free_part->index;
1052 new_part->header.signature = sig;
1053 new_part->header.length = size;
1054 strncpy(new_part->header.name, name, 12);
1055 new_part->header.checksum = nvram_checksum(&new_part->header);
1057 rc = nvram_write_header(new_part);
1059 pr_err("nvram_create_os_partition: nvram_write_header "
1060 "failed (%d)\n", rc);
1063 list_add_tail(&new_part->partition, &free_part->partition);
1065 /* Adjust or remove the partition we stole the space from */
1066 if (free_part->header.length > size) {
1067 free_part->index += size * NVRAM_BLOCK_LEN;
1068 free_part->header.length -= size;
1069 free_part->header.checksum = nvram_checksum(&free_part->header);
1070 rc = nvram_write_header(free_part);
1072 pr_err("nvram_create_os_partition: nvram_write_header "
1073 "failed (%d)\n", rc);
1077 list_del(&free_part->partition);
1081 /* Clear the new partition */
1082 for (tmp_index = new_part->index + NVRAM_HEADER_LEN;
1083 tmp_index < ((size - 1) * NVRAM_BLOCK_LEN);
1084 tmp_index += NVRAM_BLOCK_LEN) {
1085 rc = ppc_md.nvram_write(nv_init_vals, NVRAM_BLOCK_LEN, &tmp_index);
1087 pr_err("nvram_create_partition: nvram_write failed (%d)\n", rc);
1092 return new_part->index + NVRAM_HEADER_LEN;
1096 * nvram_get_partition_size - Get the data size of an nvram partition
1097 * @data_index: This is the offset of the start of the data of
1098 * the partition. The same value that is returned by
1099 * nvram_create_partition().
1101 int nvram_get_partition_size(loff_t data_index)
1103 struct nvram_partition *part;
1105 list_for_each_entry(part, &nvram_partitions, partition) {
1106 if (part->index + NVRAM_HEADER_LEN == data_index)
1107 return (part->header.length - 1) * NVRAM_BLOCK_LEN;
1114 * nvram_find_partition - Find an nvram partition by signature and name
1115 * @name: Name of the partition or NULL for any name
1116 * @sig: Signature to test against
1117 * @out_size: if non-NULL, returns the size of the data part of the partition
1119 loff_t nvram_find_partition(const char *name, int sig, int *out_size)
1121 struct nvram_partition *p;
1123 list_for_each_entry(p, &nvram_partitions, partition) {
1124 if (p->header.signature == sig &&
1125 (!name || !strncmp(p->header.name, name, 12))) {
1127 *out_size = (p->header.length - 1) *
1129 return p->index + NVRAM_HEADER_LEN;
1135 int __init nvram_scan_partitions(void)
1137 loff_t cur_index = 0;
1138 struct nvram_header phead;
1139 struct nvram_partition * tmp_part;
1140 unsigned char c_sum;
1145 if (ppc_md.nvram_size == NULL || ppc_md.nvram_size() <= 0)
1147 total_size = ppc_md.nvram_size();
1149 header = kmalloc(NVRAM_HEADER_LEN, GFP_KERNEL);
1151 printk(KERN_ERR "nvram_scan_partitions: Failed kmalloc\n");
1155 while (cur_index < total_size) {
1157 err = ppc_md.nvram_read(header, NVRAM_HEADER_LEN, &cur_index);
1158 if (err != NVRAM_HEADER_LEN) {
1159 printk(KERN_ERR "nvram_scan_partitions: Error parsing "
1160 "nvram partitions\n");
1164 cur_index -= NVRAM_HEADER_LEN; /* nvram_read will advance us */
1166 memcpy(&phead, header, NVRAM_HEADER_LEN);
1168 phead.length = be16_to_cpu(phead.length);
1171 c_sum = nvram_checksum(&phead);
1172 if (c_sum != phead.checksum) {
1173 printk(KERN_WARNING "WARNING: nvram partition checksum"
1174 " was %02x, should be %02x!\n",
1175 phead.checksum, c_sum);
1176 printk(KERN_WARNING "Terminating nvram partition scan\n");
1179 if (!phead.length) {
1180 printk(KERN_WARNING "WARNING: nvram corruption "
1181 "detected: 0-length partition\n");
1184 tmp_part = kmalloc(sizeof(struct nvram_partition), GFP_KERNEL);
1187 printk(KERN_ERR "nvram_scan_partitions: kmalloc failed\n");
1191 memcpy(&tmp_part->header, &phead, NVRAM_HEADER_LEN);
1192 tmp_part->index = cur_index;
1193 list_add_tail(&tmp_part->partition, &nvram_partitions);
1195 cur_index += phead.length * NVRAM_BLOCK_LEN;
1200 nvram_print_partitions("NVRAM Partitions");
1208 static int __init nvram_init(void)
1212 BUILD_BUG_ON(NVRAM_BLOCK_LEN != 16);
1214 if (ppc_md.nvram_size == NULL || ppc_md.nvram_size() <= 0)
1217 rc = misc_register(&nvram_dev);
1219 printk(KERN_ERR "nvram_init: failed to register device\n");
1226 static void __exit nvram_cleanup(void)
1228 misc_deregister( &nvram_dev );
1231 module_init(nvram_init);
1232 module_exit(nvram_cleanup);
1233 MODULE_LICENSE("GPL");