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("1.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
;
70 static char image_type
[MAX_IMAGE_LENGTH
] = "mono";
71 module_param_string(image_type
, image_type
, sizeof(image_type
), 0);
72 MODULE_PARM_DESC(image_type
, "BIOS image type. choose- mono or packet");
75 struct list_head list
;
81 static struct packet_data packet_data_head
;
83 static struct platform_device
*rbu_device
;
85 static dma_addr_t dell_rbu_dmaaddr
;
87 static void init_packet_head(void)
89 INIT_LIST_HEAD(&packet_data_head
.list
);
90 rbu_data
.packet_write_count
= 0;
91 rbu_data
.packet_read_count
= 0;
92 rbu_data
.num_packets
= 0;
93 rbu_data
.packetsize
= 0;
96 static int fill_last_packet(void *data
, size_t length
)
98 struct list_head
*ptemp_list
;
99 struct packet_data
*packet
= NULL
;
100 int packet_count
= 0;
102 pr_debug("fill_last_packet: entry \n");
104 if (!rbu_data
.num_packets
) {
105 pr_debug("fill_last_packet: num_packets=0\n");
109 packet_count
= rbu_data
.num_packets
;
111 ptemp_list
= (&packet_data_head
.list
)->prev
;
113 packet
= list_entry(ptemp_list
, struct packet_data
, list
);
115 if ((rbu_data
.packet_write_count
+ length
) > rbu_data
.packetsize
) {
116 pr_debug("dell_rbu:%s: packet size data "
117 "overrun\n", __FUNCTION__
);
121 pr_debug("fill_last_packet : buffer = %p\n", packet
->data
);
123 memcpy((packet
->data
+ rbu_data
.packet_write_count
), data
, length
);
125 if ((rbu_data
.packet_write_count
+ length
) == rbu_data
.packetsize
) {
127 * this was the last data chunk in the packet
128 * so reinitialize the packet data counter to zero
130 rbu_data
.packet_write_count
= 0;
132 rbu_data
.packet_write_count
+= length
;
134 pr_debug("fill_last_packet: exit \n");
138 static int create_packet(size_t length
)
140 struct packet_data
*newpacket
;
143 pr_debug("create_packet: entry \n");
145 if (!rbu_data
.packetsize
) {
146 pr_debug("create_packet: packetsize not specified\n");
150 newpacket
= kmalloc(sizeof(struct packet_data
), GFP_KERNEL
);
153 "dell_rbu:%s: failed to allocate new "
154 "packet\n", __FUNCTION__
);
158 ordernum
= get_order(length
);
160 * there is no upper limit on memory
161 * address for packetized mechanism
163 newpacket
->data
= (unsigned char *)__get_free_pages(GFP_KERNEL
,
166 pr_debug("create_packet: newpacket %p\n", newpacket
->data
);
168 if (!newpacket
->data
) {
170 "dell_rbu:%s: failed to allocate new "
171 "packet\n", __FUNCTION__
);
176 newpacket
->ordernum
= ordernum
;
177 ++rbu_data
.num_packets
;
179 * initialize the newly created packet headers
181 INIT_LIST_HEAD(&newpacket
->list
);
182 list_add_tail(&newpacket
->list
, &packet_data_head
.list
);
184 * packets have fixed size
186 newpacket
->length
= rbu_data
.packetsize
;
188 pr_debug("create_packet: exit \n");
193 static int packetize_data(void *data
, size_t length
)
197 if (!rbu_data
.packet_write_count
) {
198 if ((rc
= create_packet(length
)))
201 if ((rc
= fill_last_packet(data
, length
)))
208 do_packet_read(char *data
, struct list_head
*ptemp_list
,
209 int length
, int bytes_read
, int *list_read_count
)
212 struct packet_data
*newpacket
= NULL
;
213 int bytes_copied
= 0;
216 newpacket
= list_entry(ptemp_list
, struct packet_data
, list
);
217 *list_read_count
+= newpacket
->length
;
219 if (*list_read_count
> bytes_read
) {
220 /* point to the start of unread data */
221 j
= newpacket
->length
- (*list_read_count
- bytes_read
);
222 /* point to the offset in the packet buffer */
223 ptemp_buf
= (u8
*) newpacket
->data
+ j
;
225 * check if there is enough room in
226 * * the incoming buffer
228 if (length
> (*list_read_count
- bytes_read
))
230 * copy what ever is there in this
233 bytes_copied
= (*list_read_count
- bytes_read
);
235 /* copy the remaining */
236 bytes_copied
= length
;
237 memcpy(data
, ptemp_buf
, bytes_copied
);
242 static int packet_read_list(char *data
, size_t * pread_length
)
244 struct list_head
*ptemp_list
;
246 int bytes_copied
= 0;
248 int remaining_bytes
= 0;
251 /* check if we have any packets */
252 if (0 == rbu_data
.num_packets
)
255 remaining_bytes
= *pread_length
;
256 bytes_read
= rbu_data
.packet_read_count
;
258 ptemp_list
= (&packet_data_head
.list
)->next
;
259 while (!list_empty(ptemp_list
)) {
260 bytes_copied
= do_packet_read(pdest
, ptemp_list
,
261 remaining_bytes
, bytes_read
,
263 remaining_bytes
-= bytes_copied
;
264 bytes_read
+= bytes_copied
;
265 pdest
+= bytes_copied
;
267 * check if we reached end of buffer before reaching the
270 if (remaining_bytes
== 0)
273 ptemp_list
= ptemp_list
->next
;
275 /*finally set the bytes read */
276 *pread_length
= bytes_read
- rbu_data
.packet_read_count
;
277 rbu_data
.packet_read_count
= bytes_read
;
281 static void packet_empty_list(void)
283 struct list_head
*ptemp_list
;
284 struct list_head
*pnext_list
;
285 struct packet_data
*newpacket
;
287 ptemp_list
= (&packet_data_head
.list
)->next
;
288 while (!list_empty(ptemp_list
)) {
290 list_entry(ptemp_list
, struct packet_data
, list
);
291 pnext_list
= ptemp_list
->next
;
292 list_del(ptemp_list
);
293 ptemp_list
= pnext_list
;
295 * zero out the RBU packet memory before freeing
296 * to make sure there are no stale RBU packets left in memory
298 memset(newpacket
->data
, 0, rbu_data
.packetsize
);
299 free_pages((unsigned long)newpacket
->data
,
300 newpacket
->ordernum
);
303 rbu_data
.packet_write_count
= 0;
304 rbu_data
.packet_read_count
= 0;
305 rbu_data
.num_packets
= 0;
306 rbu_data
.packetsize
= 0;
310 * img_update_free: Frees the buffer allocated for storing BIOS image
311 * Always called with lock held and returned with lock held
313 static void img_update_free(void)
315 if (!rbu_data
.image_update_buffer
)
318 * zero out this buffer before freeing it to get rid of any stale
319 * BIOS image copied in memory.
321 memset(rbu_data
.image_update_buffer
, 0,
322 rbu_data
.image_update_buffer_size
);
323 if (rbu_data
.dma_alloc
== 1)
324 dma_free_coherent(NULL
, rbu_data
.bios_image_size
,
325 rbu_data
.image_update_buffer
,
328 free_pages((unsigned long)rbu_data
.image_update_buffer
,
329 rbu_data
.image_update_ordernum
);
332 * Re-initialize the rbu_data variables after a free
334 rbu_data
.image_update_ordernum
= -1;
335 rbu_data
.image_update_buffer
= NULL
;
336 rbu_data
.image_update_buffer_size
= 0;
337 rbu_data
.bios_image_size
= 0;
338 rbu_data
.dma_alloc
= 0;
342 * img_update_realloc: This function allocates the contiguous pages to
343 * accommodate the requested size of data. The memory address and size
344 * values are stored globally and on every call to this function the new
345 * size is checked to see if more data is required than the existing size.
346 * If true the previous memory is freed and new allocation is done to
347 * accommodate the new size. If the incoming size is less then than the
348 * already allocated size, then that memory is reused. This function is
349 * called with lock held and returns with lock held.
351 static int img_update_realloc(unsigned long size
)
353 unsigned char *image_update_buffer
= NULL
;
355 unsigned long img_buf_phys_addr
;
360 * check if the buffer of sufficient size has been
363 if (rbu_data
.image_update_buffer_size
>= size
) {
365 * check for corruption
367 if ((size
!= 0) && (rbu_data
.image_update_buffer
== NULL
)) {
368 printk(KERN_ERR
"dell_rbu:%s: corruption "
369 "check failed\n", __FUNCTION__
);
373 * we have a valid pre-allocated buffer with
380 * free any previously allocated buffer
384 spin_unlock(&rbu_data
.lock
);
386 ordernum
= get_order(size
);
387 image_update_buffer
=
388 (unsigned char *)__get_free_pages(GFP_KERNEL
, ordernum
);
391 (unsigned long)virt_to_phys(image_update_buffer
);
393 if (img_buf_phys_addr
> BIOS_SCAN_LIMIT
) {
394 free_pages((unsigned long)image_update_buffer
, ordernum
);
396 image_update_buffer
= dma_alloc_coherent(NULL
, size
,
402 spin_lock(&rbu_data
.lock
);
404 if (image_update_buffer
!= NULL
) {
405 rbu_data
.image_update_buffer
= image_update_buffer
;
406 rbu_data
.image_update_buffer_size
= size
;
407 rbu_data
.bios_image_size
=
408 rbu_data
.image_update_buffer_size
;
409 rbu_data
.image_update_ordernum
= ordernum
;
410 rbu_data
.dma_alloc
= dma_alloc
;
413 pr_debug("Not enough memory for image update:"
414 "size = %ld\n", size
);
421 static ssize_t
read_packet_data(char *buffer
, loff_t pos
, size_t count
)
426 char *ptempBuf
= buffer
;
427 unsigned long imagesize
;
429 /* check to see if we have something to return */
430 if (rbu_data
.num_packets
== 0) {
431 pr_debug("read_packet_data: no packets written\n");
433 goto read_rbu_data_exit
;
436 imagesize
= rbu_data
.num_packets
* rbu_data
.packetsize
;
438 if (pos
> imagesize
) {
440 printk(KERN_WARNING
"dell_rbu:read_packet_data: "
442 goto read_rbu_data_exit
;
445 bytes_left
= imagesize
- pos
;
446 data_length
= min(bytes_left
, count
);
448 if ((retval
= packet_read_list(ptempBuf
, &data_length
)) < 0)
449 goto read_rbu_data_exit
;
451 if ((pos
+ count
) > imagesize
) {
452 rbu_data
.packet_read_count
= 0;
453 /* this was the last copy */
462 static ssize_t
read_rbu_mono_data(char *buffer
, loff_t pos
, size_t count
)
464 unsigned char *ptemp
= NULL
;
465 size_t bytes_left
= 0;
466 size_t data_length
= 0;
467 ssize_t ret_count
= 0;
469 /* check to see if we have something to return */
470 if ((rbu_data
.image_update_buffer
== NULL
) ||
471 (rbu_data
.bios_image_size
== 0)) {
472 pr_debug("read_rbu_data_mono: image_update_buffer %p ,"
473 "bios_image_size %lu\n",
474 rbu_data
.image_update_buffer
,
475 rbu_data
.bios_image_size
);
477 goto read_rbu_data_exit
;
480 if (pos
> rbu_data
.bios_image_size
) {
482 goto read_rbu_data_exit
;
485 bytes_left
= rbu_data
.bios_image_size
- pos
;
486 data_length
= min(bytes_left
, count
);
488 ptemp
= rbu_data
.image_update_buffer
;
489 memcpy(buffer
, (ptemp
+ pos
), data_length
);
491 if ((pos
+ count
) > rbu_data
.bios_image_size
)
492 /* this was the last copy */
493 ret_count
= bytes_left
;
501 read_rbu_data(struct kobject
*kobj
, char *buffer
, loff_t pos
, size_t count
)
503 ssize_t ret_count
= 0;
505 spin_lock(&rbu_data
.lock
);
507 if (!strcmp(image_type
, "mono"))
508 ret_count
= read_rbu_mono_data(buffer
, pos
, count
);
509 else if (!strcmp(image_type
, "packet"))
510 ret_count
= read_packet_data(buffer
, pos
, count
);
512 pr_debug("read_rbu_data: invalid image type specified\n");
514 spin_unlock(&rbu_data
.lock
);
519 read_rbu_image_type(struct kobject
*kobj
, char *buffer
, loff_t pos
,
524 size
= sprintf(buffer
, "%s\n", image_type
);
529 write_rbu_image_type(struct kobject
*kobj
, char *buffer
, loff_t pos
,
533 spin_lock(&rbu_data
.lock
);
535 if (strlen(buffer
) < MAX_IMAGE_LENGTH
)
536 sscanf(buffer
, "%s", image_type
);
538 printk(KERN_WARNING
"dell_rbu: image_type is invalid"
539 "max chars = %d, \n incoming str--%s-- \n",
540 MAX_IMAGE_LENGTH
, buffer
);
542 /* we must free all previous allocations */
546 spin_unlock(&rbu_data
.lock
);
551 static struct bin_attribute rbu_data_attr
= {
552 .attr
= {.name
= "data",.owner
= THIS_MODULE
,.mode
= 0444},
553 .read
= read_rbu_data
,
556 static struct bin_attribute rbu_image_type_attr
= {
557 .attr
= {.name
= "image_type",.owner
= THIS_MODULE
,.mode
= 0644},
558 .read
= read_rbu_image_type
,
559 .write
= write_rbu_image_type
,
562 static void callbackfn_rbu(const struct firmware
*fw
, void *context
)
566 if (!fw
|| !fw
->size
)
569 spin_lock(&rbu_data
.lock
);
570 if (!strcmp(image_type
, "mono")) {
571 if (!img_update_realloc(fw
->size
))
572 memcpy(rbu_data
.image_update_buffer
,
574 } else if (!strcmp(image_type
, "packet")) {
575 if (!rbu_data
.packetsize
)
576 rbu_data
.packetsize
= fw
->size
;
577 else if (rbu_data
.packetsize
!= fw
->size
) {
579 rbu_data
.packetsize
= fw
->size
;
581 packetize_data(fw
->data
, fw
->size
);
583 pr_debug("invalid image type specified.\n");
584 spin_unlock(&rbu_data
.lock
);
586 rc
= request_firmware_nowait(THIS_MODULE
, FW_ACTION_NOHOTPLUG
,
587 "dell_rbu", &rbu_device
->dev
,
588 &context
, callbackfn_rbu
);
591 "dell_rbu:%s request_firmware_nowait failed"
592 " %d\n", __FUNCTION__
, rc
);
595 static int __init
dcdrbu_init(void)
598 spin_lock_init(&rbu_data
.lock
);
602 platform_device_register_simple("dell_rbu", -1, NULL
, 0);
605 "dell_rbu:%s:platform_device_register_simple "
606 "failed\n", __FUNCTION__
);
610 sysfs_create_bin_file(&rbu_device
->dev
.kobj
, &rbu_data_attr
);
611 sysfs_create_bin_file(&rbu_device
->dev
.kobj
, &rbu_image_type_attr
);
613 rc
= request_firmware_nowait(THIS_MODULE
, FW_ACTION_NOHOTPLUG
,
614 "dell_rbu", &rbu_device
->dev
,
615 &context
, callbackfn_rbu
);
617 printk(KERN_ERR
"dell_rbu:%s:request_firmware_nowait"
618 " failed %d\n", __FUNCTION__
, rc
);
624 static __exit
void dcdrbu_exit(void)
626 spin_lock(&rbu_data
.lock
);
629 spin_unlock(&rbu_data
.lock
);
630 platform_device_unregister(rbu_device
);
633 module_exit(dcdrbu_exit
);
634 module_init(dcdrbu_init
);