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
;
64 unsigned long packet_read_count
;
65 unsigned long num_packets
;
66 unsigned long packetsize
;
67 unsigned long imagesize
;
71 static char image_type
[MAX_IMAGE_LENGTH
+ 1] = "mono";
72 module_param_string(image_type
, image_type
, sizeof (image_type
), 0);
73 MODULE_PARM_DESC(image_type
,
74 "BIOS image type. choose- mono or packet or init");
76 static unsigned long allocation_floor
= 0x100000;
77 module_param(allocation_floor
, ulong
, 0644);
78 MODULE_PARM_DESC(allocation_floor
,
79 "Minimum address for allocations when using Packet mode");
82 struct list_head list
;
88 static struct packet_data packet_data_head
;
90 static struct platform_device
*rbu_device
;
92 static dma_addr_t dell_rbu_dmaaddr
;
94 static void init_packet_head(void)
96 INIT_LIST_HEAD(&packet_data_head
.list
);
97 rbu_data
.packet_read_count
= 0;
98 rbu_data
.num_packets
= 0;
99 rbu_data
.packetsize
= 0;
100 rbu_data
.imagesize
= 0;
103 static int create_packet(void *data
, size_t length
)
105 struct packet_data
*newpacket
;
108 unsigned int packet_array_size
= 0;
109 void **invalid_addr_packet_array
= NULL
;
110 void *packet_data_temp_buf
= NULL
;
111 unsigned int idx
= 0;
113 pr_debug("create_packet: entry \n");
115 if (!rbu_data
.packetsize
) {
116 pr_debug("create_packet: packetsize not specified\n");
121 spin_unlock(&rbu_data
.lock
);
123 newpacket
= kzalloc(sizeof (struct packet_data
), GFP_KERNEL
);
127 "dell_rbu:%s: failed to allocate new "
128 "packet\n", __func__
);
130 spin_lock(&rbu_data
.lock
);
134 ordernum
= get_order(length
);
137 * BIOS errata mean we cannot allocate packets below 1MB or they will
138 * be overwritten by BIOS.
140 * array to temporarily hold packets
141 * that are below the allocation floor
143 * NOTE: very simplistic because we only need the floor to be at 1MB
144 * due to BIOS errata. This shouldn't be used for higher floors
145 * or you will run out of mem trying to allocate the array.
147 packet_array_size
= max(
148 (unsigned int)(allocation_floor
/ rbu_data
.packetsize
),
150 invalid_addr_packet_array
= kcalloc(packet_array_size
, sizeof(void *),
153 if (!invalid_addr_packet_array
) {
155 "dell_rbu:%s: failed to allocate "
156 "invalid_addr_packet_array \n",
159 spin_lock(&rbu_data
.lock
);
160 goto out_alloc_packet
;
163 while (!packet_data_temp_buf
) {
164 packet_data_temp_buf
= (unsigned char *)
165 __get_free_pages(GFP_KERNEL
, ordernum
);
166 if (!packet_data_temp_buf
) {
168 "dell_rbu:%s: failed to allocate new "
169 "packet\n", __func__
);
171 spin_lock(&rbu_data
.lock
);
172 goto out_alloc_packet_array
;
175 if ((unsigned long)virt_to_phys(packet_data_temp_buf
)
176 < allocation_floor
) {
177 pr_debug("packet 0x%lx below floor at 0x%lx.\n",
178 (unsigned long)virt_to_phys(
179 packet_data_temp_buf
),
181 invalid_addr_packet_array
[idx
++] = packet_data_temp_buf
;
182 packet_data_temp_buf
= NULL
;
186 * set to uncachable or it may never get written back before reboot
188 set_memory_uc((unsigned long)packet_data_temp_buf
, 1 << ordernum
);
190 spin_lock(&rbu_data
.lock
);
192 newpacket
->data
= packet_data_temp_buf
;
194 pr_debug("create_packet: newpacket at physical addr %lx\n",
195 (unsigned long)virt_to_phys(newpacket
->data
));
197 /* packets may not have fixed size */
198 newpacket
->length
= length
;
199 newpacket
->ordernum
= ordernum
;
200 ++rbu_data
.num_packets
;
202 /* initialize the newly created packet headers */
203 INIT_LIST_HEAD(&newpacket
->list
);
204 list_add_tail(&newpacket
->list
, &packet_data_head
.list
);
206 memcpy(newpacket
->data
, data
, length
);
208 pr_debug("create_packet: exit \n");
210 out_alloc_packet_array
:
211 /* always free packet array */
213 pr_debug("freeing unused packet below floor 0x%lx.\n",
214 (unsigned long)virt_to_phys(
215 invalid_addr_packet_array
[idx
-1]));
216 free_pages((unsigned long)invalid_addr_packet_array
[idx
-1],
219 kfree(invalid_addr_packet_array
);
222 /* if error, free data */
230 static int packetize_data(const u8
*data
, size_t length
)
236 u8
*end
= (u8
*) data
+ length
;
237 pr_debug("packetize_data: data length %zd\n", length
);
238 if (!rbu_data
.packetsize
) {
240 "dell_rbu: packetsize not specified\n");
246 /* packetize the hunk */
248 if ((temp
+ rbu_data
.packetsize
) < end
)
249 packet_length
= rbu_data
.packetsize
;
251 /* this is the last packet */
252 packet_length
= end
- temp
;
256 if ((rc
= create_packet(temp
, packet_length
)))
259 pr_debug("%p:%td\n", temp
, (end
- temp
));
260 temp
+= packet_length
;
263 rbu_data
.imagesize
= length
;
268 static int do_packet_read(char *data
, struct list_head
*ptemp_list
,
269 int length
, int bytes_read
, int *list_read_count
)
272 struct packet_data
*newpacket
= NULL
;
273 int bytes_copied
= 0;
276 newpacket
= list_entry(ptemp_list
, struct packet_data
, list
);
277 *list_read_count
+= newpacket
->length
;
279 if (*list_read_count
> bytes_read
) {
280 /* point to the start of unread data */
281 j
= newpacket
->length
- (*list_read_count
- bytes_read
);
282 /* point to the offset in the packet buffer */
283 ptemp_buf
= (u8
*) newpacket
->data
+ j
;
285 * check if there is enough room in
286 * * the incoming buffer
288 if (length
> (*list_read_count
- bytes_read
))
290 * copy what ever is there in this
293 bytes_copied
= (*list_read_count
- bytes_read
);
295 /* copy the remaining */
296 bytes_copied
= length
;
297 memcpy(data
, ptemp_buf
, bytes_copied
);
302 static int packet_read_list(char *data
, size_t * pread_length
)
304 struct list_head
*ptemp_list
;
306 int bytes_copied
= 0;
308 int remaining_bytes
= 0;
311 /* check if we have any packets */
312 if (0 == rbu_data
.num_packets
)
315 remaining_bytes
= *pread_length
;
316 bytes_read
= rbu_data
.packet_read_count
;
318 ptemp_list
= (&packet_data_head
.list
)->next
;
319 while (!list_empty(ptemp_list
)) {
320 bytes_copied
= do_packet_read(pdest
, ptemp_list
,
321 remaining_bytes
, bytes_read
, &temp_count
);
322 remaining_bytes
-= bytes_copied
;
323 bytes_read
+= bytes_copied
;
324 pdest
+= bytes_copied
;
326 * check if we reached end of buffer before reaching the
329 if (remaining_bytes
== 0)
332 ptemp_list
= ptemp_list
->next
;
334 /*finally set the bytes read */
335 *pread_length
= bytes_read
- rbu_data
.packet_read_count
;
336 rbu_data
.packet_read_count
= bytes_read
;
340 static void packet_empty_list(void)
342 struct list_head
*ptemp_list
;
343 struct list_head
*pnext_list
;
344 struct packet_data
*newpacket
;
346 ptemp_list
= (&packet_data_head
.list
)->next
;
347 while (!list_empty(ptemp_list
)) {
349 list_entry(ptemp_list
, struct packet_data
, list
);
350 pnext_list
= ptemp_list
->next
;
351 list_del(ptemp_list
);
352 ptemp_list
= pnext_list
;
354 * zero out the RBU packet memory before freeing
355 * to make sure there are no stale RBU packets left in memory
357 memset(newpacket
->data
, 0, rbu_data
.packetsize
);
358 set_memory_wb((unsigned long)newpacket
->data
,
359 1 << newpacket
->ordernum
);
360 free_pages((unsigned long) newpacket
->data
,
361 newpacket
->ordernum
);
364 rbu_data
.packet_read_count
= 0;
365 rbu_data
.num_packets
= 0;
366 rbu_data
.imagesize
= 0;
370 * img_update_free: Frees the buffer allocated for storing BIOS image
371 * Always called with lock held and returned with lock held
373 static void img_update_free(void)
375 if (!rbu_data
.image_update_buffer
)
378 * zero out this buffer before freeing it to get rid of any stale
379 * BIOS image copied in memory.
381 memset(rbu_data
.image_update_buffer
, 0,
382 rbu_data
.image_update_buffer_size
);
383 if (rbu_data
.dma_alloc
== 1)
384 dma_free_coherent(NULL
, rbu_data
.bios_image_size
,
385 rbu_data
.image_update_buffer
, dell_rbu_dmaaddr
);
387 free_pages((unsigned long) rbu_data
.image_update_buffer
,
388 rbu_data
.image_update_ordernum
);
391 * Re-initialize the rbu_data variables after a free
393 rbu_data
.image_update_ordernum
= -1;
394 rbu_data
.image_update_buffer
= NULL
;
395 rbu_data
.image_update_buffer_size
= 0;
396 rbu_data
.bios_image_size
= 0;
397 rbu_data
.dma_alloc
= 0;
401 * img_update_realloc: This function allocates the contiguous pages to
402 * accommodate the requested size of data. The memory address and size
403 * values are stored globally and on every call to this function the new
404 * size is checked to see if more data is required than the existing size.
405 * If true the previous memory is freed and new allocation is done to
406 * accommodate the new size. If the incoming size is less then than the
407 * already allocated size, then that memory is reused. This function is
408 * called with lock held and returns with lock held.
410 static int img_update_realloc(unsigned long size
)
412 unsigned char *image_update_buffer
= NULL
;
414 unsigned long img_buf_phys_addr
;
419 * check if the buffer of sufficient size has been
422 if (rbu_data
.image_update_buffer_size
>= size
) {
424 * check for corruption
426 if ((size
!= 0) && (rbu_data
.image_update_buffer
== NULL
)) {
427 printk(KERN_ERR
"dell_rbu:%s: corruption "
428 "check failed\n", __func__
);
432 * we have a valid pre-allocated buffer with
439 * free any previously allocated buffer
443 spin_unlock(&rbu_data
.lock
);
445 ordernum
= get_order(size
);
446 image_update_buffer
=
447 (unsigned char *) __get_free_pages(GFP_KERNEL
, ordernum
);
450 (unsigned long) virt_to_phys(image_update_buffer
);
452 if (img_buf_phys_addr
> BIOS_SCAN_LIMIT
) {
453 free_pages((unsigned long) image_update_buffer
, ordernum
);
455 image_update_buffer
= dma_alloc_coherent(NULL
, size
,
456 &dell_rbu_dmaaddr
, GFP_KERNEL
);
460 spin_lock(&rbu_data
.lock
);
462 if (image_update_buffer
!= NULL
) {
463 rbu_data
.image_update_buffer
= image_update_buffer
;
464 rbu_data
.image_update_buffer_size
= size
;
465 rbu_data
.bios_image_size
=
466 rbu_data
.image_update_buffer_size
;
467 rbu_data
.image_update_ordernum
= ordernum
;
468 rbu_data
.dma_alloc
= dma_alloc
;
471 pr_debug("Not enough memory for image update:"
472 "size = %ld\n", size
);
479 static ssize_t
read_packet_data(char *buffer
, loff_t pos
, size_t count
)
484 char *ptempBuf
= buffer
;
486 /* check to see if we have something to return */
487 if (rbu_data
.num_packets
== 0) {
488 pr_debug("read_packet_data: no packets written\n");
490 goto read_rbu_data_exit
;
493 if (pos
> rbu_data
.imagesize
) {
495 printk(KERN_WARNING
"dell_rbu:read_packet_data: "
497 goto read_rbu_data_exit
;
500 bytes_left
= rbu_data
.imagesize
- pos
;
501 data_length
= min(bytes_left
, count
);
503 if ((retval
= packet_read_list(ptempBuf
, &data_length
)) < 0)
504 goto read_rbu_data_exit
;
506 if ((pos
+ count
) > rbu_data
.imagesize
) {
507 rbu_data
.packet_read_count
= 0;
508 /* this was the last copy */
517 static ssize_t
read_rbu_mono_data(char *buffer
, loff_t pos
, size_t count
)
519 /* check to see if we have something to return */
520 if ((rbu_data
.image_update_buffer
== NULL
) ||
521 (rbu_data
.bios_image_size
== 0)) {
522 pr_debug("read_rbu_data_mono: image_update_buffer %p ,"
523 "bios_image_size %lu\n",
524 rbu_data
.image_update_buffer
,
525 rbu_data
.bios_image_size
);
529 return memory_read_from_buffer(buffer
, count
, &pos
,
530 rbu_data
.image_update_buffer
, rbu_data
.bios_image_size
);
533 static ssize_t
read_rbu_data(struct file
*filp
, struct kobject
*kobj
,
534 struct bin_attribute
*bin_attr
,
535 char *buffer
, loff_t pos
, size_t count
)
537 ssize_t ret_count
= 0;
539 spin_lock(&rbu_data
.lock
);
541 if (!strcmp(image_type
, "mono"))
542 ret_count
= read_rbu_mono_data(buffer
, pos
, count
);
543 else if (!strcmp(image_type
, "packet"))
544 ret_count
= read_packet_data(buffer
, pos
, count
);
546 pr_debug("read_rbu_data: invalid image type specified\n");
548 spin_unlock(&rbu_data
.lock
);
552 static void callbackfn_rbu(const struct firmware
*fw
, void *context
)
554 rbu_data
.entry_created
= 0;
562 spin_lock(&rbu_data
.lock
);
563 if (!strcmp(image_type
, "mono")) {
564 if (!img_update_realloc(fw
->size
))
565 memcpy(rbu_data
.image_update_buffer
,
567 } else if (!strcmp(image_type
, "packet")) {
569 * we need to free previous packets if a
570 * new hunk of packets needs to be downloaded
573 if (packetize_data(fw
->data
, fw
->size
))
574 /* Incase something goes wrong when we are
575 * in middle of packetizing the data, we
576 * need to free up whatever packets might
577 * have been created before we quit.
581 pr_debug("invalid image type specified.\n");
582 spin_unlock(&rbu_data
.lock
);
584 release_firmware(fw
);
587 static ssize_t
read_rbu_image_type(struct file
*filp
, struct kobject
*kobj
,
588 struct bin_attribute
*bin_attr
,
589 char *buffer
, loff_t pos
, size_t count
)
593 size
= scnprintf(buffer
, count
, "%s\n", image_type
);
597 static ssize_t
write_rbu_image_type(struct file
*filp
, struct kobject
*kobj
,
598 struct bin_attribute
*bin_attr
,
599 char *buffer
, loff_t pos
, size_t count
)
604 spin_lock(&rbu_data
.lock
);
606 * Find the first newline or space
608 for (i
= 0; i
< count
; ++i
)
609 if (buffer
[i
] == '\n' || buffer
[i
] == ' ') {
614 buffer
[count
] = '\0';
616 if (strstr(buffer
, "mono"))
617 strcpy(image_type
, "mono");
618 else if (strstr(buffer
, "packet"))
619 strcpy(image_type
, "packet");
620 else if (strstr(buffer
, "init")) {
622 * If due to the user error the driver gets in a bad
623 * state where even though it is loaded , the
624 * /sys/class/firmware/dell_rbu entries are missing.
625 * to cover this situation the user can recreate entries
626 * by writing init to image_type.
628 if (!rbu_data
.entry_created
) {
629 spin_unlock(&rbu_data
.lock
);
630 req_firm_rc
= request_firmware_nowait(THIS_MODULE
,
631 FW_ACTION_NOHOTPLUG
, "dell_rbu",
632 &rbu_device
->dev
, GFP_KERNEL
, &context
,
636 "dell_rbu:%s request_firmware_nowait"
637 " failed %d\n", __func__
, rc
);
640 rbu_data
.entry_created
= 1;
642 spin_lock(&rbu_data
.lock
);
645 printk(KERN_WARNING
"dell_rbu: image_type is invalid\n");
646 spin_unlock(&rbu_data
.lock
);
650 /* we must free all previous allocations */
653 spin_unlock(&rbu_data
.lock
);
658 static ssize_t
read_rbu_packet_size(struct file
*filp
, struct kobject
*kobj
,
659 struct bin_attribute
*bin_attr
,
660 char *buffer
, loff_t pos
, size_t count
)
664 spin_lock(&rbu_data
.lock
);
665 size
= scnprintf(buffer
, count
, "%lu\n", rbu_data
.packetsize
);
666 spin_unlock(&rbu_data
.lock
);
671 static ssize_t
write_rbu_packet_size(struct file
*filp
, struct kobject
*kobj
,
672 struct bin_attribute
*bin_attr
,
673 char *buffer
, loff_t pos
, size_t count
)
676 spin_lock(&rbu_data
.lock
);
678 sscanf(buffer
, "%lu", &temp
);
679 if (temp
< 0xffffffff)
680 rbu_data
.packetsize
= temp
;
682 spin_unlock(&rbu_data
.lock
);
686 static struct bin_attribute rbu_data_attr
= {
687 .attr
= {.name
= "data", .mode
= 0444},
688 .read
= read_rbu_data
,
691 static struct bin_attribute rbu_image_type_attr
= {
692 .attr
= {.name
= "image_type", .mode
= 0644},
693 .read
= read_rbu_image_type
,
694 .write
= write_rbu_image_type
,
697 static struct bin_attribute rbu_packet_size_attr
= {
698 .attr
= {.name
= "packet_size", .mode
= 0644},
699 .read
= read_rbu_packet_size
,
700 .write
= write_rbu_packet_size
,
703 static int __init
dcdrbu_init(void)
706 spin_lock_init(&rbu_data
.lock
);
709 rbu_device
= platform_device_register_simple("dell_rbu", -1, NULL
, 0);
710 if (IS_ERR(rbu_device
)) {
712 "dell_rbu:%s:platform_device_register_simple "
713 "failed\n", __func__
);
714 return PTR_ERR(rbu_device
);
717 rc
= sysfs_create_bin_file(&rbu_device
->dev
.kobj
, &rbu_data_attr
);
720 rc
= sysfs_create_bin_file(&rbu_device
->dev
.kobj
, &rbu_image_type_attr
);
723 rc
= sysfs_create_bin_file(&rbu_device
->dev
.kobj
,
724 &rbu_packet_size_attr
);
728 rbu_data
.entry_created
= 0;
732 sysfs_remove_bin_file(&rbu_device
->dev
.kobj
, &rbu_image_type_attr
);
734 sysfs_remove_bin_file(&rbu_device
->dev
.kobj
, &rbu_data_attr
);
736 platform_device_unregister(rbu_device
);
740 static __exit
void dcdrbu_exit(void)
742 spin_lock(&rbu_data
.lock
);
745 spin_unlock(&rbu_data
.lock
);
746 platform_device_unregister(rbu_device
);
749 module_exit(dcdrbu_exit
);
750 module_init(dcdrbu_init
);
752 /* vim:noet:ts=8:sw=8