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/string.h>
40 #include <linux/errno.h>
41 #include <linux/blkdev.h>
42 #include <linux/platform_device.h>
43 #include <linux/spinlock.h>
44 #include <linux/moduleparam.h>
45 #include <linux/firmware.h>
46 #include <linux/dma-mapping.h>
48 MODULE_AUTHOR("Abhay Salunke <abhay_salunke@dell.com>");
49 MODULE_DESCRIPTION("Driver for updating BIOS image on DELL systems");
50 MODULE_LICENSE("GPL");
51 MODULE_VERSION("3.2");
53 #define BIOS_SCAN_LIMIT 0xffffffff
54 #define MAX_IMAGE_LENGTH 16
55 static struct _rbu_data
{
56 void *image_update_buffer
;
57 unsigned long image_update_buffer_size
;
58 unsigned long bios_image_size
;
59 int image_update_ordernum
;
62 unsigned long packet_read_count
;
63 unsigned long num_packets
;
64 unsigned long packetsize
;
65 unsigned long imagesize
;
69 static char image_type
[MAX_IMAGE_LENGTH
+ 1] = "mono";
70 module_param_string(image_type
, image_type
, sizeof (image_type
), 0);
71 MODULE_PARM_DESC(image_type
,
72 "BIOS image type. choose- mono or packet or init");
74 static unsigned long allocation_floor
= 0x100000;
75 module_param(allocation_floor
, ulong
, 0644);
76 MODULE_PARM_DESC(allocation_floor
,
77 "Minimum address for allocations when using Packet mode");
80 struct list_head list
;
86 static struct packet_data packet_data_head
;
88 static struct platform_device
*rbu_device
;
90 static dma_addr_t dell_rbu_dmaaddr
;
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
= kzalloc(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
;
183 spin_lock(&rbu_data
.lock
);
185 newpacket
->data
= packet_data_temp_buf
;
187 pr_debug("create_packet: newpacket at physical addr %lx\n",
188 (unsigned long)virt_to_phys(newpacket
->data
));
190 /* packets may not have fixed size */
191 newpacket
->length
= length
;
192 newpacket
->ordernum
= ordernum
;
193 ++rbu_data
.num_packets
;
195 /* initialize the newly created packet headers */
196 INIT_LIST_HEAD(&newpacket
->list
);
197 list_add_tail(&newpacket
->list
, &packet_data_head
.list
);
199 memcpy(newpacket
->data
, data
, length
);
201 pr_debug("create_packet: exit \n");
203 out_alloc_packet_array
:
204 /* always free packet array */
206 pr_debug("freeing unused packet below floor 0x%lx.\n",
207 (unsigned long)virt_to_phys(
208 invalid_addr_packet_array
[idx
-1]));
209 free_pages((unsigned long)invalid_addr_packet_array
[idx
-1],
212 kfree(invalid_addr_packet_array
);
215 /* if error, free data */
223 static int packetize_data(void *data
, size_t length
)
229 u8
*end
= (u8
*) data
+ length
;
230 pr_debug("packetize_data: data length %zd\n", length
);
231 if (!rbu_data
.packetsize
) {
233 "dell_rbu: packetsize not specified\n");
239 /* packetize the hunk */
241 if ((temp
+ rbu_data
.packetsize
) < end
)
242 packet_length
= rbu_data
.packetsize
;
244 /* this is the last packet */
245 packet_length
= end
- temp
;
249 if ((rc
= create_packet(temp
, packet_length
)))
252 pr_debug("%p:%td\n", temp
, (end
- temp
));
253 temp
+= packet_length
;
256 rbu_data
.imagesize
= length
;
261 static int do_packet_read(char *data
, struct list_head
*ptemp_list
,
262 int length
, int bytes_read
, int *list_read_count
)
265 struct packet_data
*newpacket
= NULL
;
266 int bytes_copied
= 0;
269 newpacket
= list_entry(ptemp_list
, struct packet_data
, list
);
270 *list_read_count
+= newpacket
->length
;
272 if (*list_read_count
> bytes_read
) {
273 /* point to the start of unread data */
274 j
= newpacket
->length
- (*list_read_count
- bytes_read
);
275 /* point to the offset in the packet buffer */
276 ptemp_buf
= (u8
*) newpacket
->data
+ j
;
278 * check if there is enough room in
279 * * the incoming buffer
281 if (length
> (*list_read_count
- bytes_read
))
283 * copy what ever is there in this
286 bytes_copied
= (*list_read_count
- bytes_read
);
288 /* copy the remaining */
289 bytes_copied
= length
;
290 memcpy(data
, ptemp_buf
, bytes_copied
);
295 static int packet_read_list(char *data
, size_t * pread_length
)
297 struct list_head
*ptemp_list
;
299 int bytes_copied
= 0;
301 int remaining_bytes
= 0;
304 /* check if we have any packets */
305 if (0 == rbu_data
.num_packets
)
308 remaining_bytes
= *pread_length
;
309 bytes_read
= rbu_data
.packet_read_count
;
311 ptemp_list
= (&packet_data_head
.list
)->next
;
312 while (!list_empty(ptemp_list
)) {
313 bytes_copied
= do_packet_read(pdest
, ptemp_list
,
314 remaining_bytes
, bytes_read
, &temp_count
);
315 remaining_bytes
-= bytes_copied
;
316 bytes_read
+= bytes_copied
;
317 pdest
+= bytes_copied
;
319 * check if we reached end of buffer before reaching the
322 if (remaining_bytes
== 0)
325 ptemp_list
= ptemp_list
->next
;
327 /*finally set the bytes read */
328 *pread_length
= bytes_read
- rbu_data
.packet_read_count
;
329 rbu_data
.packet_read_count
= bytes_read
;
333 static void packet_empty_list(void)
335 struct list_head
*ptemp_list
;
336 struct list_head
*pnext_list
;
337 struct packet_data
*newpacket
;
339 ptemp_list
= (&packet_data_head
.list
)->next
;
340 while (!list_empty(ptemp_list
)) {
342 list_entry(ptemp_list
, struct packet_data
, list
);
343 pnext_list
= ptemp_list
->next
;
344 list_del(ptemp_list
);
345 ptemp_list
= pnext_list
;
347 * zero out the RBU packet memory before freeing
348 * to make sure there are no stale RBU packets left in memory
350 memset(newpacket
->data
, 0, rbu_data
.packetsize
);
351 free_pages((unsigned long) newpacket
->data
,
352 newpacket
->ordernum
);
355 rbu_data
.packet_read_count
= 0;
356 rbu_data
.num_packets
= 0;
357 rbu_data
.imagesize
= 0;
361 * img_update_free: Frees the buffer allocated for storing BIOS image
362 * Always called with lock held and returned with lock held
364 static void img_update_free(void)
366 if (!rbu_data
.image_update_buffer
)
369 * zero out this buffer before freeing it to get rid of any stale
370 * BIOS image copied in memory.
372 memset(rbu_data
.image_update_buffer
, 0,
373 rbu_data
.image_update_buffer_size
);
374 if (rbu_data
.dma_alloc
== 1)
375 dma_free_coherent(NULL
, rbu_data
.bios_image_size
,
376 rbu_data
.image_update_buffer
, dell_rbu_dmaaddr
);
378 free_pages((unsigned long) rbu_data
.image_update_buffer
,
379 rbu_data
.image_update_ordernum
);
382 * Re-initialize the rbu_data variables after a free
384 rbu_data
.image_update_ordernum
= -1;
385 rbu_data
.image_update_buffer
= NULL
;
386 rbu_data
.image_update_buffer_size
= 0;
387 rbu_data
.bios_image_size
= 0;
388 rbu_data
.dma_alloc
= 0;
392 * img_update_realloc: This function allocates the contiguous pages to
393 * accommodate the requested size of data. The memory address and size
394 * values are stored globally and on every call to this function the new
395 * size is checked to see if more data is required than the existing size.
396 * If true the previous memory is freed and new allocation is done to
397 * accommodate the new size. If the incoming size is less then than the
398 * already allocated size, then that memory is reused. This function is
399 * called with lock held and returns with lock held.
401 static int img_update_realloc(unsigned long size
)
403 unsigned char *image_update_buffer
= NULL
;
405 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_KERNEL
, ordernum
);
441 (unsigned long) virt_to_phys(image_update_buffer
);
443 if (img_buf_phys_addr
> BIOS_SCAN_LIMIT
) {
444 free_pages((unsigned long) image_update_buffer
, ordernum
);
446 image_update_buffer
= dma_alloc_coherent(NULL
, size
,
447 &dell_rbu_dmaaddr
, GFP_KERNEL
);
451 spin_lock(&rbu_data
.lock
);
453 if (image_update_buffer
!= NULL
) {
454 rbu_data
.image_update_buffer
= image_update_buffer
;
455 rbu_data
.image_update_buffer_size
= size
;
456 rbu_data
.bios_image_size
=
457 rbu_data
.image_update_buffer_size
;
458 rbu_data
.image_update_ordernum
= ordernum
;
459 rbu_data
.dma_alloc
= dma_alloc
;
462 pr_debug("Not enough memory for image update:"
463 "size = %ld\n", size
);
470 static ssize_t
read_packet_data(char *buffer
, loff_t pos
, size_t count
)
475 char *ptempBuf
= buffer
;
477 /* check to see if we have something to return */
478 if (rbu_data
.num_packets
== 0) {
479 pr_debug("read_packet_data: no packets written\n");
481 goto read_rbu_data_exit
;
484 if (pos
> rbu_data
.imagesize
) {
486 printk(KERN_WARNING
"dell_rbu:read_packet_data: "
488 goto read_rbu_data_exit
;
491 bytes_left
= rbu_data
.imagesize
- pos
;
492 data_length
= min(bytes_left
, count
);
494 if ((retval
= packet_read_list(ptempBuf
, &data_length
)) < 0)
495 goto read_rbu_data_exit
;
497 if ((pos
+ count
) > rbu_data
.imagesize
) {
498 rbu_data
.packet_read_count
= 0;
499 /* this was the last copy */
508 static ssize_t
read_rbu_mono_data(char *buffer
, loff_t pos
, size_t count
)
510 unsigned char *ptemp
= NULL
;
511 size_t bytes_left
= 0;
512 size_t data_length
= 0;
513 ssize_t ret_count
= 0;
515 /* check to see if we have something to return */
516 if ((rbu_data
.image_update_buffer
== NULL
) ||
517 (rbu_data
.bios_image_size
== 0)) {
518 pr_debug("read_rbu_data_mono: image_update_buffer %p ,"
519 "bios_image_size %lu\n",
520 rbu_data
.image_update_buffer
,
521 rbu_data
.bios_image_size
);
523 goto read_rbu_data_exit
;
526 if (pos
> rbu_data
.bios_image_size
) {
528 goto read_rbu_data_exit
;
531 bytes_left
= rbu_data
.bios_image_size
- pos
;
532 data_length
= min(bytes_left
, count
);
534 ptemp
= rbu_data
.image_update_buffer
;
535 memcpy(buffer
, (ptemp
+ pos
), data_length
);
537 if ((pos
+ count
) > rbu_data
.bios_image_size
)
538 /* this was the last copy */
539 ret_count
= bytes_left
;
546 static ssize_t
read_rbu_data(struct kobject
*kobj
,
547 struct bin_attribute
*bin_attr
,
548 char *buffer
, loff_t pos
, size_t count
)
550 ssize_t ret_count
= 0;
552 spin_lock(&rbu_data
.lock
);
554 if (!strcmp(image_type
, "mono"))
555 ret_count
= read_rbu_mono_data(buffer
, pos
, count
);
556 else if (!strcmp(image_type
, "packet"))
557 ret_count
= read_packet_data(buffer
, pos
, count
);
559 pr_debug("read_rbu_data: invalid image type specified\n");
561 spin_unlock(&rbu_data
.lock
);
565 static void callbackfn_rbu(const struct firmware
*fw
, void *context
)
567 rbu_data
.entry_created
= 0;
569 if (!fw
|| !fw
->size
)
572 spin_lock(&rbu_data
.lock
);
573 if (!strcmp(image_type
, "mono")) {
574 if (!img_update_realloc(fw
->size
))
575 memcpy(rbu_data
.image_update_buffer
,
577 } else if (!strcmp(image_type
, "packet")) {
579 * we need to free previous packets if a
580 * new hunk of packets needs to be downloaded
583 if (packetize_data(fw
->data
, fw
->size
))
584 /* Incase something goes wrong when we are
585 * in middle of packetizing the data, we
586 * need to free up whatever packets might
587 * have been created before we quit.
591 pr_debug("invalid image type specified.\n");
592 spin_unlock(&rbu_data
.lock
);
595 static ssize_t
read_rbu_image_type(struct kobject
*kobj
,
596 struct bin_attribute
*bin_attr
,
597 char *buffer
, loff_t pos
, size_t count
)
601 size
= sprintf(buffer
, "%s\n", image_type
);
605 static ssize_t
write_rbu_image_type(struct kobject
*kobj
,
606 struct bin_attribute
*bin_attr
,
607 char *buffer
, loff_t pos
, size_t count
)
612 spin_lock(&rbu_data
.lock
);
614 * Find the first newline or space
616 for (i
= 0; i
< count
; ++i
)
617 if (buffer
[i
] == '\n' || buffer
[i
] == ' ') {
622 buffer
[count
] = '\0';
624 if (strstr(buffer
, "mono"))
625 strcpy(image_type
, "mono");
626 else if (strstr(buffer
, "packet"))
627 strcpy(image_type
, "packet");
628 else if (strstr(buffer
, "init")) {
630 * If due to the user error the driver gets in a bad
631 * state where even though it is loaded , the
632 * /sys/class/firmware/dell_rbu entries are missing.
633 * to cover this situation the user can recreate entries
634 * by writing init to image_type.
636 if (!rbu_data
.entry_created
) {
637 spin_unlock(&rbu_data
.lock
);
638 req_firm_rc
= request_firmware_nowait(THIS_MODULE
,
639 FW_ACTION_NOHOTPLUG
, "dell_rbu",
640 &rbu_device
->dev
, &context
,
644 "dell_rbu:%s request_firmware_nowait"
645 " failed %d\n", __func__
, rc
);
648 rbu_data
.entry_created
= 1;
650 spin_lock(&rbu_data
.lock
);
653 printk(KERN_WARNING
"dell_rbu: image_type is invalid\n");
654 spin_unlock(&rbu_data
.lock
);
658 /* we must free all previous allocations */
661 spin_unlock(&rbu_data
.lock
);
666 static ssize_t
read_rbu_packet_size(struct kobject
*kobj
,
667 struct bin_attribute
*bin_attr
,
668 char *buffer
, loff_t pos
, size_t count
)
672 spin_lock(&rbu_data
.lock
);
673 size
= sprintf(buffer
, "%lu\n", rbu_data
.packetsize
);
674 spin_unlock(&rbu_data
.lock
);
679 static ssize_t
write_rbu_packet_size(struct kobject
*kobj
,
680 struct bin_attribute
*bin_attr
,
681 char *buffer
, loff_t pos
, size_t count
)
684 spin_lock(&rbu_data
.lock
);
686 sscanf(buffer
, "%lu", &temp
);
687 if (temp
< 0xffffffff)
688 rbu_data
.packetsize
= temp
;
690 spin_unlock(&rbu_data
.lock
);
694 static struct bin_attribute rbu_data_attr
= {
695 .attr
= {.name
= "data", .mode
= 0444},
696 .read
= read_rbu_data
,
699 static struct bin_attribute rbu_image_type_attr
= {
700 .attr
= {.name
= "image_type", .mode
= 0644},
701 .read
= read_rbu_image_type
,
702 .write
= write_rbu_image_type
,
705 static struct bin_attribute rbu_packet_size_attr
= {
706 .attr
= {.name
= "packet_size", .mode
= 0644},
707 .read
= read_rbu_packet_size
,
708 .write
= write_rbu_packet_size
,
711 static int __init
dcdrbu_init(void)
714 spin_lock_init(&rbu_data
.lock
);
717 rbu_device
= platform_device_register_simple("dell_rbu", -1, NULL
, 0);
718 if (IS_ERR(rbu_device
)) {
720 "dell_rbu:%s:platform_device_register_simple "
721 "failed\n", __func__
);
722 return PTR_ERR(rbu_device
);
725 rc
= sysfs_create_bin_file(&rbu_device
->dev
.kobj
, &rbu_data_attr
);
728 rc
= sysfs_create_bin_file(&rbu_device
->dev
.kobj
, &rbu_image_type_attr
);
731 rc
= sysfs_create_bin_file(&rbu_device
->dev
.kobj
,
732 &rbu_packet_size_attr
);
736 rbu_data
.entry_created
= 0;
740 sysfs_remove_bin_file(&rbu_device
->dev
.kobj
, &rbu_image_type_attr
);
742 sysfs_remove_bin_file(&rbu_device
->dev
.kobj
, &rbu_data_attr
);
744 platform_device_unregister(rbu_device
);
748 static __exit
void dcdrbu_exit(void)
750 spin_lock(&rbu_data
.lock
);
753 spin_unlock(&rbu_data
.lock
);
754 platform_device_unregister(rbu_device
);
757 module_exit(dcdrbu_exit
);
758 module_init(dcdrbu_init
);
760 /* vim:noet:ts=8:sw=8