3 * Bios Update driver for Dell systems
5 * Abhay Salunke <abhay_salunke@dell.com>
7 * Copyright (C) 2005 Dell Inc.
9 * Remote BIOS Update (rbu) driver is used for updating DELL BIOS by
10 * creating entries in the /sys file systems on Linux 2.6 and higher
11 * kernels. The driver supports two mechanism to update the BIOS namely
12 * contiguous and packetized. Both these methods still require having some
13 * application to set the CMOS bit indicating the BIOS to update itself
17 * This driver writes the incoming data in a monolithic image by allocating
18 * contiguous physical pages large enough to accommodate the incoming BIOS
22 * The driver writes the incoming packet image by allocating a new packet
23 * on every time the packet data is written. This driver requires an
24 * application to break the BIOS image in to fixed sized packet chunks.
26 * See Documentation/dell_rbu.txt for more info.
28 * This program is free software; you can redistribute it and/or modify
29 * it under the terms of the GNU General Public License v2.0 as published by
30 * the Free Software Foundation
32 * This program is distributed in the hope that it will be useful,
33 * but WITHOUT ANY WARRANTY; without even the implied warranty of
34 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35 * GNU General Public License for more details.
37 #include <linux/init.h>
38 #include <linux/module.h>
39 #include <linux/slab.h>
40 #include <linux/string.h>
41 #include <linux/errno.h>
42 #include <linux/blkdev.h>
43 #include <linux/platform_device.h>
44 #include <linux/spinlock.h>
45 #include <linux/moduleparam.h>
46 #include <linux/firmware.h>
47 #include <linux/dma-mapping.h>
48 #include <asm/set_memory.h>
50 MODULE_AUTHOR("Abhay Salunke <abhay_salunke@dell.com>");
51 MODULE_DESCRIPTION("Driver for updating BIOS image on DELL systems");
52 MODULE_LICENSE("GPL");
53 MODULE_VERSION("3.2");
55 #define BIOS_SCAN_LIMIT 0xffffffff
56 #define MAX_IMAGE_LENGTH 16
57 static struct _rbu_data
{
58 void *image_update_buffer
;
59 unsigned long image_update_buffer_size
;
60 unsigned long bios_image_size
;
61 int image_update_ordernum
;
63 unsigned long packet_read_count
;
64 unsigned long num_packets
;
65 unsigned long packetsize
;
66 unsigned long imagesize
;
70 static char image_type
[MAX_IMAGE_LENGTH
+ 1] = "mono";
71 module_param_string(image_type
, image_type
, sizeof (image_type
), 0);
72 MODULE_PARM_DESC(image_type
,
73 "BIOS image type. choose- mono or packet or init");
75 static unsigned long allocation_floor
= 0x100000;
76 module_param(allocation_floor
, ulong
, 0644);
77 MODULE_PARM_DESC(allocation_floor
,
78 "Minimum address for allocations when using Packet mode");
81 struct list_head list
;
87 static struct packet_data packet_data_head
;
89 static struct platform_device
*rbu_device
;
92 static void init_packet_head(void)
94 INIT_LIST_HEAD(&packet_data_head
.list
);
95 rbu_data
.packet_read_count
= 0;
96 rbu_data
.num_packets
= 0;
97 rbu_data
.packetsize
= 0;
98 rbu_data
.imagesize
= 0;
101 static int create_packet(void *data
, size_t length
)
103 struct packet_data
*newpacket
;
106 unsigned int packet_array_size
= 0;
107 void **invalid_addr_packet_array
= NULL
;
108 void *packet_data_temp_buf
= NULL
;
109 unsigned int idx
= 0;
111 pr_debug("create_packet: entry \n");
113 if (!rbu_data
.packetsize
) {
114 pr_debug("create_packet: packetsize not specified\n");
119 spin_unlock(&rbu_data
.lock
);
121 newpacket
= kzalloc(sizeof (struct packet_data
), GFP_KERNEL
);
125 "dell_rbu:%s: failed to allocate new "
126 "packet\n", __func__
);
128 spin_lock(&rbu_data
.lock
);
132 ordernum
= get_order(length
);
135 * BIOS errata mean we cannot allocate packets below 1MB or they will
136 * be overwritten by BIOS.
138 * array to temporarily hold packets
139 * that are below the allocation floor
141 * NOTE: very simplistic because we only need the floor to be at 1MB
142 * due to BIOS errata. This shouldn't be used for higher floors
143 * or you will run out of mem trying to allocate the array.
145 packet_array_size
= max(
146 (unsigned int)(allocation_floor
/ rbu_data
.packetsize
),
148 invalid_addr_packet_array
= kcalloc(packet_array_size
, sizeof(void *),
151 if (!invalid_addr_packet_array
) {
153 "dell_rbu:%s: failed to allocate "
154 "invalid_addr_packet_array \n",
157 spin_lock(&rbu_data
.lock
);
158 goto out_alloc_packet
;
161 while (!packet_data_temp_buf
) {
162 packet_data_temp_buf
= (unsigned char *)
163 __get_free_pages(GFP_KERNEL
, ordernum
);
164 if (!packet_data_temp_buf
) {
166 "dell_rbu:%s: failed to allocate new "
167 "packet\n", __func__
);
169 spin_lock(&rbu_data
.lock
);
170 goto out_alloc_packet_array
;
173 if ((unsigned long)virt_to_phys(packet_data_temp_buf
)
174 < allocation_floor
) {
175 pr_debug("packet 0x%lx below floor at 0x%lx.\n",
176 (unsigned long)virt_to_phys(
177 packet_data_temp_buf
),
179 invalid_addr_packet_array
[idx
++] = packet_data_temp_buf
;
180 packet_data_temp_buf
= NULL
;
184 * set to uncachable or it may never get written back before reboot
186 set_memory_uc((unsigned long)packet_data_temp_buf
, 1 << ordernum
);
188 spin_lock(&rbu_data
.lock
);
190 newpacket
->data
= packet_data_temp_buf
;
192 pr_debug("create_packet: newpacket at physical addr %lx\n",
193 (unsigned long)virt_to_phys(newpacket
->data
));
195 /* packets may not have fixed size */
196 newpacket
->length
= length
;
197 newpacket
->ordernum
= ordernum
;
198 ++rbu_data
.num_packets
;
200 /* initialize the newly created packet headers */
201 INIT_LIST_HEAD(&newpacket
->list
);
202 list_add_tail(&newpacket
->list
, &packet_data_head
.list
);
204 memcpy(newpacket
->data
, data
, length
);
206 pr_debug("create_packet: exit \n");
208 out_alloc_packet_array
:
209 /* always free packet array */
211 pr_debug("freeing unused packet below floor 0x%lx.\n",
212 (unsigned long)virt_to_phys(
213 invalid_addr_packet_array
[idx
-1]));
214 free_pages((unsigned long)invalid_addr_packet_array
[idx
-1],
217 kfree(invalid_addr_packet_array
);
220 /* if error, free data */
228 static int packetize_data(const u8
*data
, size_t length
)
234 u8
*end
= (u8
*) data
+ length
;
235 pr_debug("packetize_data: data length %zd\n", length
);
236 if (!rbu_data
.packetsize
) {
238 "dell_rbu: packetsize not specified\n");
244 /* packetize the hunk */
246 if ((temp
+ rbu_data
.packetsize
) < end
)
247 packet_length
= rbu_data
.packetsize
;
249 /* this is the last packet */
250 packet_length
= end
- temp
;
254 if ((rc
= create_packet(temp
, packet_length
)))
257 pr_debug("%p:%td\n", temp
, (end
- temp
));
258 temp
+= packet_length
;
261 rbu_data
.imagesize
= length
;
266 static int do_packet_read(char *data
, struct list_head
*ptemp_list
,
267 int length
, int bytes_read
, int *list_read_count
)
270 struct packet_data
*newpacket
= NULL
;
271 int bytes_copied
= 0;
274 newpacket
= list_entry(ptemp_list
, struct packet_data
, list
);
275 *list_read_count
+= newpacket
->length
;
277 if (*list_read_count
> bytes_read
) {
278 /* point to the start of unread data */
279 j
= newpacket
->length
- (*list_read_count
- bytes_read
);
280 /* point to the offset in the packet buffer */
281 ptemp_buf
= (u8
*) newpacket
->data
+ j
;
283 * check if there is enough room in
284 * * the incoming buffer
286 if (length
> (*list_read_count
- bytes_read
))
288 * copy what ever is there in this
291 bytes_copied
= (*list_read_count
- bytes_read
);
293 /* copy the remaining */
294 bytes_copied
= length
;
295 memcpy(data
, ptemp_buf
, bytes_copied
);
300 static int packet_read_list(char *data
, size_t * pread_length
)
302 struct list_head
*ptemp_list
;
304 int bytes_copied
= 0;
306 int remaining_bytes
= 0;
309 /* check if we have any packets */
310 if (0 == rbu_data
.num_packets
)
313 remaining_bytes
= *pread_length
;
314 bytes_read
= rbu_data
.packet_read_count
;
316 ptemp_list
= (&packet_data_head
.list
)->next
;
317 while (!list_empty(ptemp_list
)) {
318 bytes_copied
= do_packet_read(pdest
, ptemp_list
,
319 remaining_bytes
, bytes_read
, &temp_count
);
320 remaining_bytes
-= bytes_copied
;
321 bytes_read
+= bytes_copied
;
322 pdest
+= bytes_copied
;
324 * check if we reached end of buffer before reaching the
327 if (remaining_bytes
== 0)
330 ptemp_list
= ptemp_list
->next
;
332 /*finally set the bytes read */
333 *pread_length
= bytes_read
- rbu_data
.packet_read_count
;
334 rbu_data
.packet_read_count
= bytes_read
;
338 static void packet_empty_list(void)
340 struct list_head
*ptemp_list
;
341 struct list_head
*pnext_list
;
342 struct packet_data
*newpacket
;
344 ptemp_list
= (&packet_data_head
.list
)->next
;
345 while (!list_empty(ptemp_list
)) {
347 list_entry(ptemp_list
, struct packet_data
, list
);
348 pnext_list
= ptemp_list
->next
;
349 list_del(ptemp_list
);
350 ptemp_list
= pnext_list
;
352 * zero out the RBU packet memory before freeing
353 * to make sure there are no stale RBU packets left in memory
355 memset(newpacket
->data
, 0, rbu_data
.packetsize
);
356 set_memory_wb((unsigned long)newpacket
->data
,
357 1 << newpacket
->ordernum
);
358 free_pages((unsigned long) newpacket
->data
,
359 newpacket
->ordernum
);
362 rbu_data
.packet_read_count
= 0;
363 rbu_data
.num_packets
= 0;
364 rbu_data
.imagesize
= 0;
368 * img_update_free: Frees the buffer allocated for storing BIOS image
369 * Always called with lock held and returned with lock held
371 static void img_update_free(void)
373 if (!rbu_data
.image_update_buffer
)
376 * zero out this buffer before freeing it to get rid of any stale
377 * BIOS image copied in memory.
379 memset(rbu_data
.image_update_buffer
, 0,
380 rbu_data
.image_update_buffer_size
);
381 free_pages((unsigned long) rbu_data
.image_update_buffer
,
382 rbu_data
.image_update_ordernum
);
385 * Re-initialize the rbu_data variables after a free
387 rbu_data
.image_update_ordernum
= -1;
388 rbu_data
.image_update_buffer
= NULL
;
389 rbu_data
.image_update_buffer_size
= 0;
390 rbu_data
.bios_image_size
= 0;
394 * img_update_realloc: This function allocates the contiguous pages to
395 * accommodate the requested size of data. The memory address and size
396 * values are stored globally and on every call to this function the new
397 * size is checked to see if more data is required than the existing size.
398 * If true the previous memory is freed and new allocation is done to
399 * accommodate the new size. If the incoming size is less then than the
400 * already allocated size, then that memory is reused. This function is
401 * called with lock held and returns with lock held.
403 static int img_update_realloc(unsigned long size
)
405 unsigned char *image_update_buffer
= NULL
;
406 unsigned long img_buf_phys_addr
;
410 * check if the buffer of sufficient size has been
413 if (rbu_data
.image_update_buffer_size
>= size
) {
415 * check for corruption
417 if ((size
!= 0) && (rbu_data
.image_update_buffer
== NULL
)) {
418 printk(KERN_ERR
"dell_rbu:%s: corruption "
419 "check failed\n", __func__
);
423 * we have a valid pre-allocated buffer with
430 * free any previously allocated buffer
434 spin_unlock(&rbu_data
.lock
);
436 ordernum
= get_order(size
);
437 image_update_buffer
=
438 (unsigned char *)__get_free_pages(GFP_DMA32
, ordernum
);
439 spin_lock(&rbu_data
.lock
);
440 if (!image_update_buffer
) {
441 pr_debug("Not enough memory for image update:"
442 "size = %ld\n", size
);
446 img_buf_phys_addr
= (unsigned long)virt_to_phys(image_update_buffer
);
447 if (WARN_ON_ONCE(img_buf_phys_addr
> BIOS_SCAN_LIMIT
))
448 return -EINVAL
; /* can't happen per definition */
450 rbu_data
.image_update_buffer
= image_update_buffer
;
451 rbu_data
.image_update_buffer_size
= size
;
452 rbu_data
.bios_image_size
= rbu_data
.image_update_buffer_size
;
453 rbu_data
.image_update_ordernum
= ordernum
;
457 static ssize_t
read_packet_data(char *buffer
, loff_t pos
, size_t count
)
462 char *ptempBuf
= buffer
;
464 /* check to see if we have something to return */
465 if (rbu_data
.num_packets
== 0) {
466 pr_debug("read_packet_data: no packets written\n");
468 goto read_rbu_data_exit
;
471 if (pos
> rbu_data
.imagesize
) {
473 printk(KERN_WARNING
"dell_rbu:read_packet_data: "
475 goto read_rbu_data_exit
;
478 bytes_left
= rbu_data
.imagesize
- pos
;
479 data_length
= min(bytes_left
, count
);
481 if ((retval
= packet_read_list(ptempBuf
, &data_length
)) < 0)
482 goto read_rbu_data_exit
;
484 if ((pos
+ count
) > rbu_data
.imagesize
) {
485 rbu_data
.packet_read_count
= 0;
486 /* this was the last copy */
495 static ssize_t
read_rbu_mono_data(char *buffer
, loff_t pos
, size_t count
)
497 /* check to see if we have something to return */
498 if ((rbu_data
.image_update_buffer
== NULL
) ||
499 (rbu_data
.bios_image_size
== 0)) {
500 pr_debug("read_rbu_data_mono: image_update_buffer %p ,"
501 "bios_image_size %lu\n",
502 rbu_data
.image_update_buffer
,
503 rbu_data
.bios_image_size
);
507 return memory_read_from_buffer(buffer
, count
, &pos
,
508 rbu_data
.image_update_buffer
, rbu_data
.bios_image_size
);
511 static ssize_t
read_rbu_data(struct file
*filp
, struct kobject
*kobj
,
512 struct bin_attribute
*bin_attr
,
513 char *buffer
, loff_t pos
, size_t count
)
515 ssize_t ret_count
= 0;
517 spin_lock(&rbu_data
.lock
);
519 if (!strcmp(image_type
, "mono"))
520 ret_count
= read_rbu_mono_data(buffer
, pos
, count
);
521 else if (!strcmp(image_type
, "packet"))
522 ret_count
= read_packet_data(buffer
, pos
, count
);
524 pr_debug("read_rbu_data: invalid image type specified\n");
526 spin_unlock(&rbu_data
.lock
);
530 static void callbackfn_rbu(const struct firmware
*fw
, void *context
)
532 rbu_data
.entry_created
= 0;
540 spin_lock(&rbu_data
.lock
);
541 if (!strcmp(image_type
, "mono")) {
542 if (!img_update_realloc(fw
->size
))
543 memcpy(rbu_data
.image_update_buffer
,
545 } else if (!strcmp(image_type
, "packet")) {
547 * we need to free previous packets if a
548 * new hunk of packets needs to be downloaded
551 if (packetize_data(fw
->data
, fw
->size
))
552 /* Incase something goes wrong when we are
553 * in middle of packetizing the data, we
554 * need to free up whatever packets might
555 * have been created before we quit.
559 pr_debug("invalid image type specified.\n");
560 spin_unlock(&rbu_data
.lock
);
562 release_firmware(fw
);
565 static ssize_t
read_rbu_image_type(struct file
*filp
, struct kobject
*kobj
,
566 struct bin_attribute
*bin_attr
,
567 char *buffer
, loff_t pos
, size_t count
)
571 size
= scnprintf(buffer
, count
, "%s\n", image_type
);
575 static ssize_t
write_rbu_image_type(struct file
*filp
, struct kobject
*kobj
,
576 struct bin_attribute
*bin_attr
,
577 char *buffer
, loff_t pos
, size_t count
)
582 spin_lock(&rbu_data
.lock
);
584 * Find the first newline or space
586 for (i
= 0; i
< count
; ++i
)
587 if (buffer
[i
] == '\n' || buffer
[i
] == ' ') {
592 buffer
[count
] = '\0';
594 if (strstr(buffer
, "mono"))
595 strcpy(image_type
, "mono");
596 else if (strstr(buffer
, "packet"))
597 strcpy(image_type
, "packet");
598 else if (strstr(buffer
, "init")) {
600 * If due to the user error the driver gets in a bad
601 * state where even though it is loaded , the
602 * /sys/class/firmware/dell_rbu entries are missing.
603 * to cover this situation the user can recreate entries
604 * by writing init to image_type.
606 if (!rbu_data
.entry_created
) {
607 spin_unlock(&rbu_data
.lock
);
608 req_firm_rc
= request_firmware_nowait(THIS_MODULE
,
609 FW_ACTION_NOHOTPLUG
, "dell_rbu",
610 &rbu_device
->dev
, GFP_KERNEL
, &context
,
614 "dell_rbu:%s request_firmware_nowait"
615 " failed %d\n", __func__
, rc
);
618 rbu_data
.entry_created
= 1;
620 spin_lock(&rbu_data
.lock
);
623 printk(KERN_WARNING
"dell_rbu: image_type is invalid\n");
624 spin_unlock(&rbu_data
.lock
);
628 /* we must free all previous allocations */
631 spin_unlock(&rbu_data
.lock
);
636 static ssize_t
read_rbu_packet_size(struct file
*filp
, struct kobject
*kobj
,
637 struct bin_attribute
*bin_attr
,
638 char *buffer
, loff_t pos
, size_t count
)
642 spin_lock(&rbu_data
.lock
);
643 size
= scnprintf(buffer
, count
, "%lu\n", rbu_data
.packetsize
);
644 spin_unlock(&rbu_data
.lock
);
649 static ssize_t
write_rbu_packet_size(struct file
*filp
, struct kobject
*kobj
,
650 struct bin_attribute
*bin_attr
,
651 char *buffer
, loff_t pos
, size_t count
)
654 spin_lock(&rbu_data
.lock
);
656 sscanf(buffer
, "%lu", &temp
);
657 if (temp
< 0xffffffff)
658 rbu_data
.packetsize
= temp
;
660 spin_unlock(&rbu_data
.lock
);
664 static struct bin_attribute rbu_data_attr
= {
665 .attr
= {.name
= "data", .mode
= 0444},
666 .read
= read_rbu_data
,
669 static struct bin_attribute rbu_image_type_attr
= {
670 .attr
= {.name
= "image_type", .mode
= 0644},
671 .read
= read_rbu_image_type
,
672 .write
= write_rbu_image_type
,
675 static struct bin_attribute rbu_packet_size_attr
= {
676 .attr
= {.name
= "packet_size", .mode
= 0644},
677 .read
= read_rbu_packet_size
,
678 .write
= write_rbu_packet_size
,
681 static int __init
dcdrbu_init(void)
684 spin_lock_init(&rbu_data
.lock
);
687 rbu_device
= platform_device_register_simple("dell_rbu", -1, NULL
, 0);
688 if (IS_ERR(rbu_device
)) {
690 "dell_rbu:%s:platform_device_register_simple "
691 "failed\n", __func__
);
692 return PTR_ERR(rbu_device
);
695 rc
= sysfs_create_bin_file(&rbu_device
->dev
.kobj
, &rbu_data_attr
);
698 rc
= sysfs_create_bin_file(&rbu_device
->dev
.kobj
, &rbu_image_type_attr
);
701 rc
= sysfs_create_bin_file(&rbu_device
->dev
.kobj
,
702 &rbu_packet_size_attr
);
706 rbu_data
.entry_created
= 0;
710 sysfs_remove_bin_file(&rbu_device
->dev
.kobj
, &rbu_image_type_attr
);
712 sysfs_remove_bin_file(&rbu_device
->dev
.kobj
, &rbu_data_attr
);
714 platform_device_unregister(rbu_device
);
718 static __exit
void dcdrbu_exit(void)
720 spin_lock(&rbu_data
.lock
);
723 spin_unlock(&rbu_data
.lock
);
724 platform_device_unregister(rbu_device
);
727 module_exit(dcdrbu_exit
);
728 module_init(dcdrbu_init
);
730 /* vim:noet:ts=8:sw=8