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>
49 MODULE_AUTHOR("Abhay Salunke <abhay_salunke@dell.com>");
50 MODULE_DESCRIPTION("Driver for updating BIOS image on DELL systems");
51 MODULE_LICENSE("GPL");
52 MODULE_VERSION("3.2");
54 #define BIOS_SCAN_LIMIT 0xffffffff
55 #define MAX_IMAGE_LENGTH 16
56 static struct _rbu_data {
57 void *image_update_buffer;
58 unsigned long image_update_buffer_size;
59 unsigned long bios_image_size;
60 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;
91 static dma_addr_t dell_rbu_dmaaddr;
93 static void init_packet_head(void)
95 INIT_LIST_HEAD(&packet_data_head.list);
96 rbu_data.packet_read_count = 0;
97 rbu_data.num_packets = 0;
98 rbu_data.packetsize = 0;
99 rbu_data.imagesize = 0;
102 static int create_packet(void *data, size_t length)
104 struct packet_data *newpacket;
107 unsigned int packet_array_size = 0;
108 void **invalid_addr_packet_array = NULL;
109 void *packet_data_temp_buf = NULL;
110 unsigned int idx = 0;
112 pr_debug("create_packet: entry \n");
114 if (!rbu_data.packetsize) {
115 pr_debug("create_packet: packetsize not specified\n");
120 spin_unlock(&rbu_data.lock);
122 newpacket = kzalloc(sizeof (struct packet_data), GFP_KERNEL);
126 "dell_rbu:%s: failed to allocate new "
127 "packet\n", __func__);
129 spin_lock(&rbu_data.lock);
133 ordernum = get_order(length);
136 * BIOS errata mean we cannot allocate packets below 1MB or they will
137 * be overwritten by BIOS.
139 * array to temporarily hold packets
140 * that are below the allocation floor
142 * NOTE: very simplistic because we only need the floor to be at 1MB
143 * due to BIOS errata. This shouldn't be used for higher floors
144 * or you will run out of mem trying to allocate the array.
146 packet_array_size = max(
147 (unsigned int)(allocation_floor / rbu_data.packetsize),
149 invalid_addr_packet_array = kzalloc(packet_array_size * sizeof(void*),
152 if (!invalid_addr_packet_array) {
154 "dell_rbu:%s: failed to allocate "
155 "invalid_addr_packet_array \n",
158 spin_lock(&rbu_data.lock);
159 goto out_alloc_packet;
162 while (!packet_data_temp_buf) {
163 packet_data_temp_buf = (unsigned char *)
164 __get_free_pages(GFP_KERNEL, ordernum);
165 if (!packet_data_temp_buf) {
167 "dell_rbu:%s: failed to allocate new "
168 "packet\n", __func__);
170 spin_lock(&rbu_data.lock);
171 goto out_alloc_packet_array;
174 if (ordernum && xen_create_contiguous_region(
175 (unsigned long)packet_data_temp_buf, ordernum, 0)) {
176 free_pages((unsigned long)packet_data_temp_buf,
178 pr_warning("dell_rbu:%s: failed to adjust new "
179 "packet\n", __func__);
181 spin_lock(&rbu_data.lock);
182 goto out_alloc_packet_array;
186 if ((unsigned long)virt_to_bus(packet_data_temp_buf)
187 < allocation_floor) {
190 xen_destroy_contiguous_region(
191 (unsigned long)packet_data_temp_buf,
194 pr_debug("packet 0x%lx below floor at 0x%lx.\n",
195 (unsigned long)virt_to_phys(
196 packet_data_temp_buf),
198 invalid_addr_packet_array[idx++] = packet_data_temp_buf;
199 packet_data_temp_buf = NULL;
202 spin_lock(&rbu_data.lock);
204 newpacket->data = packet_data_temp_buf;
206 pr_debug("create_packet: newpacket at physical addr %lx\n",
207 (unsigned long)virt_to_bus(newpacket->data));
209 /* packets may not have fixed size */
210 newpacket->length = length;
211 newpacket->ordernum = ordernum;
212 ++rbu_data.num_packets;
214 /* initialize the newly created packet headers */
215 INIT_LIST_HEAD(&newpacket->list);
216 list_add_tail(&newpacket->list, &packet_data_head.list);
218 memcpy(newpacket->data, data, length);
220 pr_debug("create_packet: exit \n");
222 out_alloc_packet_array:
223 /* always free packet array */
225 pr_debug("freeing unused packet below floor 0x%lx.\n",
226 (unsigned long)virt_to_bus(
227 invalid_addr_packet_array[idx-1]));
228 free_pages((unsigned long)invalid_addr_packet_array[idx-1],
231 kfree(invalid_addr_packet_array);
234 /* if error, free data */
242 static int packetize_data(const u8 *data, size_t length)
248 u8 *end = (u8 *) data + length;
249 pr_debug("packetize_data: data length %zd\n", length);
250 if (!rbu_data.packetsize) {
252 "dell_rbu: packetsize not specified\n");
258 /* packetize the hunk */
260 if ((temp + rbu_data.packetsize) < end)
261 packet_length = rbu_data.packetsize;
263 /* this is the last packet */
264 packet_length = end - temp;
268 if ((rc = create_packet(temp, packet_length)))
271 pr_debug("%p:%td\n", temp, (end - temp));
272 temp += packet_length;
275 rbu_data.imagesize = length;
280 static int do_packet_read(char *data, struct list_head *ptemp_list,
281 int length, int bytes_read, int *list_read_count)
284 struct packet_data *newpacket = NULL;
285 int bytes_copied = 0;
288 newpacket = list_entry(ptemp_list, struct packet_data, list);
289 *list_read_count += newpacket->length;
291 if (*list_read_count > bytes_read) {
292 /* point to the start of unread data */
293 j = newpacket->length - (*list_read_count - bytes_read);
294 /* point to the offset in the packet buffer */
295 ptemp_buf = (u8 *) newpacket->data + j;
297 * check if there is enough room in
298 * * the incoming buffer
300 if (length > (*list_read_count - bytes_read))
302 * copy what ever is there in this
305 bytes_copied = (*list_read_count - bytes_read);
307 /* copy the remaining */
308 bytes_copied = length;
309 memcpy(data, ptemp_buf, bytes_copied);
314 static int packet_read_list(char *data, size_t * pread_length)
316 struct list_head *ptemp_list;
318 int bytes_copied = 0;
320 int remaining_bytes = 0;
323 /* check if we have any packets */
324 if (0 == rbu_data.num_packets)
327 remaining_bytes = *pread_length;
328 bytes_read = rbu_data.packet_read_count;
330 ptemp_list = (&packet_data_head.list)->next;
331 while (!list_empty(ptemp_list)) {
332 bytes_copied = do_packet_read(pdest, ptemp_list,
333 remaining_bytes, bytes_read, &temp_count);
334 remaining_bytes -= bytes_copied;
335 bytes_read += bytes_copied;
336 pdest += bytes_copied;
338 * check if we reached end of buffer before reaching the
341 if (remaining_bytes == 0)
344 ptemp_list = ptemp_list->next;
346 /*finally set the bytes read */
347 *pread_length = bytes_read - rbu_data.packet_read_count;
348 rbu_data.packet_read_count = bytes_read;
352 static void packet_empty_list(void)
354 struct list_head *ptemp_list;
355 struct list_head *pnext_list;
356 struct packet_data *newpacket;
358 ptemp_list = (&packet_data_head.list)->next;
359 while (!list_empty(ptemp_list)) {
361 list_entry(ptemp_list, struct packet_data, list);
362 pnext_list = ptemp_list->next;
363 list_del(ptemp_list);
364 ptemp_list = pnext_list;
366 * zero out the RBU packet memory before freeing
367 * to make sure there are no stale RBU packets left in memory
369 memset(newpacket->data, 0, rbu_data.packetsize);
371 if (newpacket->ordernum)
372 xen_destroy_contiguous_region(
373 (unsigned long)newpacket->data,
374 newpacket->ordernum);
377 free_pages((unsigned long) newpacket->data,
378 newpacket->ordernum);
381 rbu_data.packet_read_count = 0;
382 rbu_data.num_packets = 0;
383 rbu_data.imagesize = 0;
387 * img_update_free: Frees the buffer allocated for storing BIOS image
388 * Always called with lock held and returned with lock held
390 static void img_update_free(void)
392 if (!rbu_data.image_update_buffer)
395 * zero out this buffer before freeing it to get rid of any stale
396 * BIOS image copied in memory.
398 memset(rbu_data.image_update_buffer, 0,
399 rbu_data.image_update_buffer_size);
400 if (rbu_data.dma_alloc == 1)
401 dma_free_coherent(NULL, rbu_data.bios_image_size,
402 rbu_data.image_update_buffer, dell_rbu_dmaaddr);
404 free_pages((unsigned long) rbu_data.image_update_buffer,
405 rbu_data.image_update_ordernum);
408 * Re-initialize the rbu_data variables after a free
410 rbu_data.image_update_ordernum = -1;
411 rbu_data.image_update_buffer = NULL;
412 rbu_data.image_update_buffer_size = 0;
413 rbu_data.bios_image_size = 0;
414 rbu_data.dma_alloc = 0;
418 * img_update_realloc: This function allocates the contiguous pages to
419 * accommodate the requested size of data. The memory address and size
420 * values are stored globally and on every call to this function the new
421 * size is checked to see if more data is required than the existing size.
422 * If true the previous memory is freed and new allocation is done to
423 * accommodate the new size. If the incoming size is less then than the
424 * already allocated size, then that memory is reused. This function is
425 * called with lock held and returns with lock held.
427 static int img_update_realloc(unsigned long size)
429 unsigned char *image_update_buffer = NULL;
432 unsigned long img_buf_phys_addr;
438 * check if the buffer of sufficient size has been
441 if (rbu_data.image_update_buffer_size >= size) {
443 * check for corruption
445 if ((size != 0) && (rbu_data.image_update_buffer == NULL)) {
446 printk(KERN_ERR "dell_rbu:%s: corruption "
447 "check failed\n", __func__);
451 * we have a valid pre-allocated buffer with
458 * free any previously allocated buffer
462 spin_unlock(&rbu_data.lock);
465 ordernum = get_order(size);
466 image_update_buffer =
467 (unsigned char *) __get_free_pages(GFP_KERNEL, ordernum);
470 (unsigned long) virt_to_bus(image_update_buffer);
472 if (img_buf_phys_addr > BIOS_SCAN_LIMIT) {
473 free_pages((unsigned long) image_update_buffer, ordernum);
478 image_update_buffer = dma_alloc_coherent(NULL, size,
479 &dell_rbu_dmaaddr, GFP_KERNEL);
483 spin_lock(&rbu_data.lock);
485 if (image_update_buffer != NULL) {
486 rbu_data.image_update_buffer = image_update_buffer;
487 rbu_data.image_update_buffer_size = size;
488 rbu_data.bios_image_size =
489 rbu_data.image_update_buffer_size;
490 rbu_data.image_update_ordernum = ordernum;
491 rbu_data.dma_alloc = dma_alloc;
494 pr_debug("Not enough memory for image update:"
495 "size = %ld\n", size);
502 static ssize_t read_packet_data(char *buffer, loff_t pos, size_t count)
507 char *ptempBuf = buffer;
509 /* check to see if we have something to return */
510 if (rbu_data.num_packets == 0) {
511 pr_debug("read_packet_data: no packets written\n");
513 goto read_rbu_data_exit;
516 if (pos > rbu_data.imagesize) {
518 printk(KERN_WARNING "dell_rbu:read_packet_data: "
520 goto read_rbu_data_exit;
523 bytes_left = rbu_data.imagesize - pos;
524 data_length = min(bytes_left, count);
526 if ((retval = packet_read_list(ptempBuf, &data_length)) < 0)
527 goto read_rbu_data_exit;
529 if ((pos + count) > rbu_data.imagesize) {
530 rbu_data.packet_read_count = 0;
531 /* this was the last copy */
540 static ssize_t read_rbu_mono_data(char *buffer, loff_t pos, size_t count)
542 /* check to see if we have something to return */
543 if ((rbu_data.image_update_buffer == NULL) ||
544 (rbu_data.bios_image_size == 0)) {
545 pr_debug("read_rbu_data_mono: image_update_buffer %p ,"
546 "bios_image_size %lu\n",
547 rbu_data.image_update_buffer,
548 rbu_data.bios_image_size);
552 return memory_read_from_buffer(buffer, count, &pos,
553 rbu_data.image_update_buffer, rbu_data.bios_image_size);
556 static ssize_t read_rbu_data(struct file *filp, struct kobject *kobj,
557 struct bin_attribute *bin_attr,
558 char *buffer, loff_t pos, size_t count)
560 ssize_t ret_count = 0;
562 spin_lock(&rbu_data.lock);
564 if (!strcmp(image_type, "mono"))
565 ret_count = read_rbu_mono_data(buffer, pos, count);
566 else if (!strcmp(image_type, "packet"))
567 ret_count = read_packet_data(buffer, pos, count);
569 pr_debug("read_rbu_data: invalid image type specified\n");
571 spin_unlock(&rbu_data.lock);
575 static void callbackfn_rbu(const struct firmware *fw, void *context)
577 rbu_data.entry_created = 0;
585 spin_lock(&rbu_data.lock);
586 if (!strcmp(image_type, "mono")) {
587 if (!img_update_realloc(fw->size))
588 memcpy(rbu_data.image_update_buffer,
590 } else if (!strcmp(image_type, "packet")) {
592 * we need to free previous packets if a
593 * new hunk of packets needs to be downloaded
596 if (packetize_data(fw->data, fw->size))
597 /* Incase something goes wrong when we are
598 * in middle of packetizing the data, we
599 * need to free up whatever packets might
600 * have been created before we quit.
604 pr_debug("invalid image type specified.\n");
605 spin_unlock(&rbu_data.lock);
607 release_firmware(fw);
610 static ssize_t read_rbu_image_type(struct file *filp, struct kobject *kobj,
611 struct bin_attribute *bin_attr,
612 char *buffer, loff_t pos, size_t count)
616 size = scnprintf(buffer, count, "%s\n", image_type);
620 static ssize_t write_rbu_image_type(struct file *filp, struct kobject *kobj,
621 struct bin_attribute *bin_attr,
622 char *buffer, loff_t pos, size_t count)
627 spin_lock(&rbu_data.lock);
629 * Find the first newline or space
631 for (i = 0; i < count; ++i)
632 if (buffer[i] == '\n' || buffer[i] == ' ') {
637 buffer[count] = '\0';
639 if (strstr(buffer, "mono"))
640 strcpy(image_type, "mono");
641 else if (strstr(buffer, "packet"))
642 strcpy(image_type, "packet");
643 else if (strstr(buffer, "init")) {
645 * If due to the user error the driver gets in a bad
646 * state where even though it is loaded , the
647 * /sys/class/firmware/dell_rbu entries are missing.
648 * to cover this situation the user can recreate entries
649 * by writing init to image_type.
651 if (!rbu_data.entry_created) {
652 spin_unlock(&rbu_data.lock);
653 req_firm_rc = request_firmware_nowait(THIS_MODULE,
654 FW_ACTION_NOHOTPLUG, "dell_rbu",
655 &rbu_device->dev, GFP_KERNEL, &context,
659 "dell_rbu:%s request_firmware_nowait"
660 " failed %d\n", __func__, rc);
663 rbu_data.entry_created = 1;
665 spin_lock(&rbu_data.lock);
668 printk(KERN_WARNING "dell_rbu: image_type is invalid\n");
669 spin_unlock(&rbu_data.lock);
673 /* we must free all previous allocations */
676 spin_unlock(&rbu_data.lock);
681 static ssize_t read_rbu_packet_size(struct file *filp, struct kobject *kobj,
682 struct bin_attribute *bin_attr,
683 char *buffer, loff_t pos, size_t count)
687 spin_lock(&rbu_data.lock);
688 size = scnprintf(buffer, count, "%lu\n", rbu_data.packetsize);
689 spin_unlock(&rbu_data.lock);
694 static ssize_t write_rbu_packet_size(struct file *filp, struct kobject *kobj,
695 struct bin_attribute *bin_attr,
696 char *buffer, loff_t pos, size_t count)
699 spin_lock(&rbu_data.lock);
701 sscanf(buffer, "%lu", &temp);
702 if (temp < 0xffffffff)
703 rbu_data.packetsize = temp;
705 spin_unlock(&rbu_data.lock);
709 static struct bin_attribute rbu_data_attr = {
710 .attr = {.name = "data", .mode = 0444},
711 .read = read_rbu_data,
714 static struct bin_attribute rbu_image_type_attr = {
715 .attr = {.name = "image_type", .mode = 0644},
716 .read = read_rbu_image_type,
717 .write = write_rbu_image_type,
720 static struct bin_attribute rbu_packet_size_attr = {
721 .attr = {.name = "packet_size", .mode = 0644},
722 .read = read_rbu_packet_size,
723 .write = write_rbu_packet_size,
726 static int __init dcdrbu_init(void)
731 if (!is_initial_xendomain())
735 spin_lock_init(&rbu_data.lock);
738 rbu_device = platform_device_register_simple("dell_rbu", -1, NULL, 0);
739 if (IS_ERR(rbu_device)) {
741 "dell_rbu:%s:platform_device_register_simple "
742 "failed\n", __func__);
743 return PTR_ERR(rbu_device);
746 rc = sysfs_create_bin_file(&rbu_device->dev.kobj, &rbu_data_attr);
749 rc = sysfs_create_bin_file(&rbu_device->dev.kobj, &rbu_image_type_attr);
752 rc = sysfs_create_bin_file(&rbu_device->dev.kobj,
753 &rbu_packet_size_attr);
757 rbu_data.entry_created = 0;
761 sysfs_remove_bin_file(&rbu_device->dev.kobj, &rbu_image_type_attr);
763 sysfs_remove_bin_file(&rbu_device->dev.kobj, &rbu_data_attr);
765 platform_device_unregister(rbu_device);
769 static __exit void dcdrbu_exit(void)
771 spin_lock(&rbu_data.lock);
774 spin_unlock(&rbu_data.lock);
775 platform_device_unregister(rbu_device);
778 module_exit(dcdrbu_exit);
779 module_init(dcdrbu_init);
781 /* vim:noet:ts=8:sw=8