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/version.h>
38 #include <linux/config.h>
39 #include <linux/init.h>
40 #include <linux/module.h>
41 #include <linux/string.h>
42 #include <linux/errno.h>
43 #include <linux/blkdev.h>
44 #include <linux/device.h>
45 #include <linux/spinlock.h>
46 #include <linux/moduleparam.h>
47 #include <linux/firmware.h>
48 #include <linux/dma-mapping.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("2.0");
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 packet_write_count
;
66 unsigned long num_packets
;
67 unsigned long packetsize
;
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
, "BIOS image type. choose- mono or packet");
76 struct list_head list
;
82 static struct packet_data packet_data_head
;
84 static struct platform_device
*rbu_device
;
86 static dma_addr_t dell_rbu_dmaaddr
;
88 static void init_packet_head(void)
90 INIT_LIST_HEAD(&packet_data_head
.list
);
91 rbu_data
.packet_write_count
= 0;
92 rbu_data
.packet_read_count
= 0;
93 rbu_data
.num_packets
= 0;
94 rbu_data
.packetsize
= 0;
97 static int fill_last_packet(void *data
, size_t length
)
99 struct list_head
*ptemp_list
;
100 struct packet_data
*packet
= NULL
;
101 int packet_count
= 0;
103 pr_debug("fill_last_packet: entry \n");
105 if (!rbu_data
.num_packets
) {
106 pr_debug("fill_last_packet: num_packets=0\n");
110 packet_count
= rbu_data
.num_packets
;
112 ptemp_list
= (&packet_data_head
.list
)->prev
;
114 packet
= list_entry(ptemp_list
, struct packet_data
, list
);
116 if ((rbu_data
.packet_write_count
+ length
) > rbu_data
.packetsize
) {
117 pr_debug("dell_rbu:%s: packet size data "
118 "overrun\n", __FUNCTION__
);
122 pr_debug("fill_last_packet : buffer = %p\n", packet
->data
);
124 memcpy((packet
->data
+ rbu_data
.packet_write_count
), data
, length
);
126 if ((rbu_data
.packet_write_count
+ length
) == rbu_data
.packetsize
) {
128 * this was the last data chunk in the packet
129 * so reinitialize the packet data counter to zero
131 rbu_data
.packet_write_count
= 0;
133 rbu_data
.packet_write_count
+= length
;
135 pr_debug("fill_last_packet: exit \n");
139 static int create_packet(size_t length
)
141 struct packet_data
*newpacket
;
144 pr_debug("create_packet: entry \n");
146 if (!rbu_data
.packetsize
) {
147 pr_debug("create_packet: packetsize not specified\n");
150 spin_unlock(&rbu_data
.lock
);
151 newpacket
= kmalloc(sizeof (struct packet_data
), GFP_KERNEL
);
152 spin_lock(&rbu_data
.lock
);
156 "dell_rbu:%s: failed to allocate new "
157 "packet\n", __FUNCTION__
);
161 ordernum
= get_order(length
);
163 * there is no upper limit on memory
164 * address for packetized mechanism
166 spin_unlock(&rbu_data
.lock
);
167 newpacket
->data
= (unsigned char *) __get_free_pages(GFP_KERNEL
,
169 spin_lock(&rbu_data
.lock
);
171 pr_debug("create_packet: newpacket %p\n", newpacket
->data
);
173 if (!newpacket
->data
) {
175 "dell_rbu:%s: failed to allocate new "
176 "packet\n", __FUNCTION__
);
181 newpacket
->ordernum
= ordernum
;
182 ++rbu_data
.num_packets
;
184 * initialize the newly created packet headers
186 INIT_LIST_HEAD(&newpacket
->list
);
187 list_add_tail(&newpacket
->list
, &packet_data_head
.list
);
189 * packets have fixed size
191 newpacket
->length
= rbu_data
.packetsize
;
193 pr_debug("create_packet: exit \n");
198 static int packetize_data(void *data
, size_t length
)
202 if (!rbu_data
.packet_write_count
) {
203 if ((rc
= create_packet(length
)))
206 if ((rc
= fill_last_packet(data
, length
)))
212 static int do_packet_read(char *data
, struct list_head
*ptemp_list
,
213 int length
, int bytes_read
, int *list_read_count
)
216 struct packet_data
*newpacket
= NULL
;
217 int bytes_copied
= 0;
220 newpacket
= list_entry(ptemp_list
, struct packet_data
, list
);
221 *list_read_count
+= newpacket
->length
;
223 if (*list_read_count
> bytes_read
) {
224 /* point to the start of unread data */
225 j
= newpacket
->length
- (*list_read_count
- bytes_read
);
226 /* point to the offset in the packet buffer */
227 ptemp_buf
= (u8
*) newpacket
->data
+ j
;
229 * check if there is enough room in
230 * * the incoming buffer
232 if (length
> (*list_read_count
- bytes_read
))
234 * copy what ever is there in this
237 bytes_copied
= (*list_read_count
- bytes_read
);
239 /* copy the remaining */
240 bytes_copied
= length
;
241 memcpy(data
, ptemp_buf
, bytes_copied
);
246 static int packet_read_list(char *data
, size_t *pread_length
)
248 struct list_head
*ptemp_list
;
250 int bytes_copied
= 0;
252 int remaining_bytes
= 0;
255 /* check if we have any packets */
256 if (0 == rbu_data
.num_packets
)
259 remaining_bytes
= *pread_length
;
260 bytes_read
= rbu_data
.packet_read_count
;
262 ptemp_list
= (&packet_data_head
.list
)->next
;
263 while (!list_empty(ptemp_list
)) {
264 bytes_copied
= do_packet_read(pdest
, ptemp_list
,
265 remaining_bytes
, bytes_read
, &temp_count
);
266 remaining_bytes
-= bytes_copied
;
267 bytes_read
+= bytes_copied
;
268 pdest
+= bytes_copied
;
270 * check if we reached end of buffer before reaching the
273 if (remaining_bytes
== 0)
276 ptemp_list
= ptemp_list
->next
;
278 /*finally set the bytes read */
279 *pread_length
= bytes_read
- rbu_data
.packet_read_count
;
280 rbu_data
.packet_read_count
= bytes_read
;
284 static void packet_empty_list(void)
286 struct list_head
*ptemp_list
;
287 struct list_head
*pnext_list
;
288 struct packet_data
*newpacket
;
290 ptemp_list
= (&packet_data_head
.list
)->next
;
291 while (!list_empty(ptemp_list
)) {
293 list_entry(ptemp_list
, struct packet_data
, list
);
294 pnext_list
= ptemp_list
->next
;
295 list_del(ptemp_list
);
296 ptemp_list
= pnext_list
;
298 * zero out the RBU packet memory before freeing
299 * to make sure there are no stale RBU packets left in memory
301 memset(newpacket
->data
, 0, rbu_data
.packetsize
);
302 free_pages((unsigned long) newpacket
->data
,
303 newpacket
->ordernum
);
306 rbu_data
.packet_write_count
= 0;
307 rbu_data
.packet_read_count
= 0;
308 rbu_data
.num_packets
= 0;
309 rbu_data
.packetsize
= 0;
313 * img_update_free: Frees the buffer allocated for storing BIOS image
314 * Always called with lock held and returned with lock held
316 static void img_update_free(void)
318 if (!rbu_data
.image_update_buffer
)
321 * zero out this buffer before freeing it to get rid of any stale
322 * BIOS image copied in memory.
324 memset(rbu_data
.image_update_buffer
, 0,
325 rbu_data
.image_update_buffer_size
);
326 if (rbu_data
.dma_alloc
== 1)
327 dma_free_coherent(NULL
, rbu_data
.bios_image_size
,
328 rbu_data
.image_update_buffer
, dell_rbu_dmaaddr
);
330 free_pages((unsigned long) rbu_data
.image_update_buffer
,
331 rbu_data
.image_update_ordernum
);
334 * Re-initialize the rbu_data variables after a free
336 rbu_data
.image_update_ordernum
= -1;
337 rbu_data
.image_update_buffer
= NULL
;
338 rbu_data
.image_update_buffer_size
= 0;
339 rbu_data
.bios_image_size
= 0;
340 rbu_data
.dma_alloc
= 0;
344 * img_update_realloc: This function allocates the contiguous pages to
345 * accommodate the requested size of data. The memory address and size
346 * values are stored globally and on every call to this function the new
347 * size is checked to see if more data is required than the existing size.
348 * If true the previous memory is freed and new allocation is done to
349 * accommodate the new size. If the incoming size is less then than the
350 * already allocated size, then that memory is reused. This function is
351 * called with lock held and returns with lock held.
353 static int img_update_realloc(unsigned long size
)
355 unsigned char *image_update_buffer
= NULL
;
357 unsigned long img_buf_phys_addr
;
362 * check if the buffer of sufficient size has been
365 if (rbu_data
.image_update_buffer_size
>= size
) {
367 * check for corruption
369 if ((size
!= 0) && (rbu_data
.image_update_buffer
== NULL
)) {
370 printk(KERN_ERR
"dell_rbu:%s: corruption "
371 "check failed\n", __FUNCTION__
);
375 * we have a valid pre-allocated buffer with
382 * free any previously allocated buffer
386 spin_unlock(&rbu_data
.lock
);
388 ordernum
= get_order(size
);
389 image_update_buffer
=
390 (unsigned char *) __get_free_pages(GFP_KERNEL
, ordernum
);
393 (unsigned long) virt_to_phys(image_update_buffer
);
395 if (img_buf_phys_addr
> BIOS_SCAN_LIMIT
) {
396 free_pages((unsigned long) image_update_buffer
, ordernum
);
398 image_update_buffer
= dma_alloc_coherent(NULL
, size
,
399 &dell_rbu_dmaaddr
, GFP_KERNEL
);
403 spin_lock(&rbu_data
.lock
);
405 if (image_update_buffer
!= NULL
) {
406 rbu_data
.image_update_buffer
= image_update_buffer
;
407 rbu_data
.image_update_buffer_size
= size
;
408 rbu_data
.bios_image_size
=
409 rbu_data
.image_update_buffer_size
;
410 rbu_data
.image_update_ordernum
= ordernum
;
411 rbu_data
.dma_alloc
= dma_alloc
;
414 pr_debug("Not enough memory for image update:"
415 "size = %ld\n", size
);
422 static ssize_t
read_packet_data(char *buffer
, loff_t pos
, size_t count
)
427 char *ptempBuf
= buffer
;
428 unsigned long imagesize
;
430 /* check to see if we have something to return */
431 if (rbu_data
.num_packets
== 0) {
432 pr_debug("read_packet_data: no packets written\n");
434 goto read_rbu_data_exit
;
437 imagesize
= rbu_data
.num_packets
* rbu_data
.packetsize
;
439 if (pos
> imagesize
) {
441 printk(KERN_WARNING
"dell_rbu:read_packet_data: "
443 goto read_rbu_data_exit
;
446 bytes_left
= imagesize
- pos
;
447 data_length
= min(bytes_left
, count
);
449 if ((retval
= packet_read_list(ptempBuf
, &data_length
)) < 0)
450 goto read_rbu_data_exit
;
452 if ((pos
+ count
) > imagesize
) {
453 rbu_data
.packet_read_count
= 0;
454 /* this was the last copy */
463 static ssize_t
read_rbu_mono_data(char *buffer
, loff_t pos
, size_t count
)
465 unsigned char *ptemp
= NULL
;
466 size_t bytes_left
= 0;
467 size_t data_length
= 0;
468 ssize_t ret_count
= 0;
470 /* check to see if we have something to return */
471 if ((rbu_data
.image_update_buffer
== NULL
) ||
472 (rbu_data
.bios_image_size
== 0)) {
473 pr_debug("read_rbu_data_mono: image_update_buffer %p ,"
474 "bios_image_size %lu\n",
475 rbu_data
.image_update_buffer
,
476 rbu_data
.bios_image_size
);
478 goto read_rbu_data_exit
;
481 if (pos
> rbu_data
.bios_image_size
) {
483 goto read_rbu_data_exit
;
486 bytes_left
= rbu_data
.bios_image_size
- pos
;
487 data_length
= min(bytes_left
, count
);
489 ptemp
= rbu_data
.image_update_buffer
;
490 memcpy(buffer
, (ptemp
+ pos
), data_length
);
492 if ((pos
+ count
) > rbu_data
.bios_image_size
)
493 /* this was the last copy */
494 ret_count
= bytes_left
;
501 static ssize_t
read_rbu_data(struct kobject
*kobj
, char *buffer
,
502 loff_t pos
, size_t count
)
504 ssize_t ret_count
= 0;
506 spin_lock(&rbu_data
.lock
);
508 if (!strcmp(image_type
, "mono"))
509 ret_count
= read_rbu_mono_data(buffer
, pos
, count
);
510 else if (!strcmp(image_type
, "packet"))
511 ret_count
= read_packet_data(buffer
, pos
, count
);
513 pr_debug("read_rbu_data: invalid image type specified\n");
515 spin_unlock(&rbu_data
.lock
);
519 static void callbackfn_rbu(const struct firmware
*fw
, void *context
)
523 if (!fw
|| !fw
->size
) {
524 rbu_data
.entry_created
= 0;
528 spin_lock(&rbu_data
.lock
);
529 if (!strcmp(image_type
, "mono")) {
530 if (!img_update_realloc(fw
->size
))
531 memcpy(rbu_data
.image_update_buffer
,
533 } else if (!strcmp(image_type
, "packet")) {
534 if (!rbu_data
.packetsize
)
535 rbu_data
.packetsize
= fw
->size
;
536 else if (rbu_data
.packetsize
!= fw
->size
) {
538 rbu_data
.packetsize
= fw
->size
;
540 packetize_data(fw
->data
, fw
->size
);
542 pr_debug("invalid image type specified.\n");
543 spin_unlock(&rbu_data
.lock
);
545 rc
= request_firmware_nowait(THIS_MODULE
, FW_ACTION_NOHOTPLUG
,
546 "dell_rbu", &rbu_device
->dev
, &context
, callbackfn_rbu
);
549 "dell_rbu:%s request_firmware_nowait failed"
550 " %d\n", __FUNCTION__
, rc
);
552 rbu_data
.entry_created
= 1;
555 static ssize_t
read_rbu_image_type(struct kobject
*kobj
, char *buffer
,
556 loff_t pos
, size_t count
)
560 size
= sprintf(buffer
, "%s\n", image_type
);
564 static ssize_t
write_rbu_image_type(struct kobject
*kobj
, char *buffer
,
565 loff_t pos
, size_t count
)
570 spin_lock(&rbu_data
.lock
);
572 * Find the first newline or space
574 for (i
= 0; i
< count
; ++i
)
575 if (buffer
[i
] == '\n' || buffer
[i
] == ' ') {
580 buffer
[count
] = '\0';
582 if (strstr(buffer
, "mono"))
583 strcpy(image_type
, "mono");
584 else if (strstr(buffer
, "packet"))
585 strcpy(image_type
, "packet");
586 else if (strstr(buffer
, "init")) {
588 * If due to the user error the driver gets in a bad
589 * state where even though it is loaded , the
590 * /sys/class/firmware/dell_rbu entries are missing.
591 * to cover this situation the user can recreate entries
592 * by writing init to image_type.
594 if (!rbu_data
.entry_created
) {
595 spin_unlock(&rbu_data
.lock
);
596 req_firm_rc
= request_firmware_nowait(THIS_MODULE
,
597 FW_ACTION_NOHOTPLUG
, "dell_rbu",
598 &rbu_device
->dev
, &context
,
602 "dell_rbu:%s request_firmware_nowait"
603 " failed %d\n", __FUNCTION__
, rc
);
606 rbu_data
.entry_created
= 1;
608 spin_lock(&rbu_data
.lock
);
611 printk(KERN_WARNING
"dell_rbu: image_type is invalid\n");
612 spin_unlock(&rbu_data
.lock
);
616 /* we must free all previous allocations */
619 spin_unlock(&rbu_data
.lock
);
624 static struct bin_attribute rbu_data_attr
= {
627 .owner
= THIS_MODULE
,
630 .read
= read_rbu_data
,
633 static struct bin_attribute rbu_image_type_attr
= {
635 .name
= "image_type",
636 .owner
= THIS_MODULE
,
639 .read
= read_rbu_image_type
,
640 .write
= write_rbu_image_type
,
643 static int __init
dcdrbu_init(void)
646 spin_lock_init(&rbu_data
.lock
);
650 platform_device_register_simple("dell_rbu", -1, NULL
, 0);
653 "dell_rbu:%s:platform_device_register_simple "
654 "failed\n", __FUNCTION__
);
658 sysfs_create_bin_file(&rbu_device
->dev
.kobj
, &rbu_data_attr
);
659 sysfs_create_bin_file(&rbu_device
->dev
.kobj
, &rbu_image_type_attr
);
661 rc
= request_firmware_nowait(THIS_MODULE
, FW_ACTION_NOHOTPLUG
,
662 "dell_rbu", &rbu_device
->dev
, &context
, callbackfn_rbu
);
664 printk(KERN_ERR
"dell_rbu:%s:request_firmware_nowait"
665 " failed %d\n", __FUNCTION__
, rc
);
667 rbu_data
.entry_created
= 1;
673 static __exit
void dcdrbu_exit(void)
675 spin_lock(&rbu_data
.lock
);
678 spin_unlock(&rbu_data
.lock
);
679 platform_device_unregister(rbu_device
);
682 module_exit(dcdrbu_exit
);
683 module_init(dcdrbu_init
);