1 // SPDX-License-Identifier: GPL-2.0-only
4 * Bios Update driver for Dell systems
6 * Abhay Salunke <abhay_salunke@dell.com>
8 * Copyright (C) 2005 Dell Inc.
10 * Remote BIOS Update (rbu) driver is used for updating DELL BIOS by
11 * creating entries in the /sys file systems on Linux 2.6 and higher
12 * kernels. The driver supports two mechanism to update the BIOS namely
13 * contiguous and packetized. Both these methods still require having some
14 * application to set the CMOS bit indicating the BIOS to update itself
18 * This driver writes the incoming data in a monolithic image by allocating
19 * contiguous physical pages large enough to accommodate the incoming BIOS
23 * The driver writes the incoming packet image by allocating a new packet
24 * on every time the packet data is written. This driver requires an
25 * application to break the BIOS image in to fixed sized packet chunks.
27 * See Documentation/admin-guide/dell_rbu.rst for more info.
30 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
32 #include <linux/init.h>
33 #include <linux/module.h>
34 #include <linux/slab.h>
35 #include <linux/string.h>
36 #include <linux/errno.h>
37 #include <linux/blkdev.h>
38 #include <linux/platform_device.h>
39 #include <linux/spinlock.h>
40 #include <linux/moduleparam.h>
41 #include <linux/firmware.h>
42 #include <linux/dma-mapping.h>
43 #include <asm/set_memory.h>
45 MODULE_AUTHOR("Abhay Salunke <abhay_salunke@dell.com>");
46 MODULE_DESCRIPTION("Driver for updating BIOS image on DELL systems");
47 MODULE_LICENSE("GPL");
48 MODULE_VERSION("3.2");
50 #define BIOS_SCAN_LIMIT 0xffffffff
51 #define MAX_IMAGE_LENGTH 16
52 static struct _rbu_data {
53 void *image_update_buffer;
54 unsigned long image_update_buffer_size;
55 unsigned long bios_image_size;
56 int image_update_ordernum;
58 unsigned long packet_read_count;
59 unsigned long num_packets;
60 unsigned long packetsize;
61 unsigned long imagesize;
65 static char image_type[MAX_IMAGE_LENGTH + 1] = "mono";
66 module_param_string(image_type, image_type, sizeof (image_type), 0);
67 MODULE_PARM_DESC(image_type, "BIOS image type. choose- mono or packet or init");
69 static unsigned long allocation_floor = 0x100000;
70 module_param(allocation_floor, ulong, 0644);
71 MODULE_PARM_DESC(allocation_floor, "Minimum address for allocations when using Packet mode");
74 struct list_head list;
80 static struct packet_data packet_data_head;
82 static struct platform_device *rbu_device;
85 static void init_packet_head(void)
87 INIT_LIST_HEAD(&packet_data_head.list);
88 rbu_data.packet_read_count = 0;
89 rbu_data.num_packets = 0;
90 rbu_data.packetsize = 0;
91 rbu_data.imagesize = 0;
94 static int create_packet(void *data, size_t length)
96 struct packet_data *newpacket;
99 unsigned int packet_array_size = 0;
100 void **invalid_addr_packet_array = NULL;
101 void *packet_data_temp_buf = NULL;
102 unsigned int idx = 0;
106 if (!rbu_data.packetsize) {
107 pr_debug("packetsize not specified\n");
112 spin_unlock(&rbu_data.lock);
114 newpacket = kzalloc(sizeof (struct packet_data), GFP_KERNEL);
117 pr_warn("failed to allocate new packet\n");
119 spin_lock(&rbu_data.lock);
123 ordernum = get_order(length);
126 * BIOS errata mean we cannot allocate packets below 1MB or they will
127 * be overwritten by BIOS.
129 * array to temporarily hold packets
130 * that are below the allocation floor
132 * NOTE: very simplistic because we only need the floor to be at 1MB
133 * due to BIOS errata. This shouldn't be used for higher floors
134 * or you will run out of mem trying to allocate the array.
136 packet_array_size = max_t(unsigned int, allocation_floor / rbu_data.packetsize, 1);
137 invalid_addr_packet_array = kcalloc(packet_array_size, sizeof(void *),
140 if (!invalid_addr_packet_array) {
141 pr_warn("failed to allocate invalid_addr_packet_array\n");
143 spin_lock(&rbu_data.lock);
144 goto out_alloc_packet;
147 while (!packet_data_temp_buf) {
148 packet_data_temp_buf = (unsigned char *)
149 __get_free_pages(GFP_KERNEL, ordernum);
150 if (!packet_data_temp_buf) {
151 pr_warn("failed to allocate new packet\n");
153 spin_lock(&rbu_data.lock);
154 goto out_alloc_packet_array;
157 if ((unsigned long)virt_to_phys(packet_data_temp_buf)
158 < allocation_floor) {
159 pr_debug("packet 0x%lx below floor at 0x%lx\n",
160 (unsigned long)virt_to_phys(
161 packet_data_temp_buf),
163 invalid_addr_packet_array[idx++] = packet_data_temp_buf;
164 packet_data_temp_buf = NULL;
168 * set to uncachable or it may never get written back before reboot
170 set_memory_uc((unsigned long)packet_data_temp_buf, 1 << ordernum);
172 spin_lock(&rbu_data.lock);
174 newpacket->data = packet_data_temp_buf;
176 pr_debug("newpacket at physical addr %lx\n",
177 (unsigned long)virt_to_phys(newpacket->data));
179 /* packets may not have fixed size */
180 newpacket->length = length;
181 newpacket->ordernum = ordernum;
182 ++rbu_data.num_packets;
184 /* initialize the newly created packet headers */
185 INIT_LIST_HEAD(&newpacket->list);
186 list_add_tail(&newpacket->list, &packet_data_head.list);
188 memcpy(newpacket->data, data, length);
192 out_alloc_packet_array:
193 /* always free packet array */
195 pr_debug("freeing unused packet below floor 0x%lx\n",
196 (unsigned long)virt_to_phys(invalid_addr_packet_array[idx]));
197 free_pages((unsigned long)invalid_addr_packet_array[idx], ordernum);
199 kfree(invalid_addr_packet_array);
202 /* if error, free data */
210 static int packetize_data(const u8 *data, size_t length)
216 u8 *end = (u8 *) data + length;
217 pr_debug("data length %zd\n", length);
218 if (!rbu_data.packetsize) {
219 pr_warn("packetsize not specified\n");
225 /* packetize the hunk */
227 if ((temp + rbu_data.packetsize) < end)
228 packet_length = rbu_data.packetsize;
230 /* this is the last packet */
231 packet_length = end - temp;
235 if ((rc = create_packet(temp, packet_length)))
238 pr_debug("%p:%td\n", temp, (end - temp));
239 temp += packet_length;
242 rbu_data.imagesize = length;
247 static int do_packet_read(char *data, struct packet_data *newpacket,
248 int length, int bytes_read, int *list_read_count)
251 int bytes_copied = 0;
254 *list_read_count += newpacket->length;
256 if (*list_read_count > bytes_read) {
257 /* point to the start of unread data */
258 j = newpacket->length - (*list_read_count - bytes_read);
259 /* point to the offset in the packet buffer */
260 ptemp_buf = (u8 *) newpacket->data + j;
262 * check if there is enough room in
263 * * the incoming buffer
265 if (length > (*list_read_count - bytes_read))
267 * copy what ever is there in this
270 bytes_copied = (*list_read_count - bytes_read);
272 /* copy the remaining */
273 bytes_copied = length;
274 memcpy(data, ptemp_buf, bytes_copied);
279 static int packet_read_list(char *data, size_t * pread_length)
281 struct packet_data *newpacket;
283 int bytes_copied = 0;
285 int remaining_bytes = 0;
288 /* check if we have any packets */
289 if (0 == rbu_data.num_packets)
292 remaining_bytes = *pread_length;
293 bytes_read = rbu_data.packet_read_count;
295 list_for_each_entry(newpacket, (&packet_data_head.list)->next, list) {
296 bytes_copied = do_packet_read(pdest, newpacket,
297 remaining_bytes, bytes_read, &temp_count);
298 remaining_bytes -= bytes_copied;
299 bytes_read += bytes_copied;
300 pdest += bytes_copied;
302 * check if we reached end of buffer before reaching the
305 if (remaining_bytes == 0)
308 /*finally set the bytes read */
309 *pread_length = bytes_read - rbu_data.packet_read_count;
310 rbu_data.packet_read_count = bytes_read;
314 static void packet_empty_list(void)
316 struct packet_data *newpacket, *tmp;
318 list_for_each_entry_safe(newpacket, tmp, (&packet_data_head.list)->next, list) {
319 list_del(&newpacket->list);
322 * zero out the RBU packet memory before freeing
323 * to make sure there are no stale RBU packets left in memory
325 memset(newpacket->data, 0, rbu_data.packetsize);
326 set_memory_wb((unsigned long)newpacket->data,
327 1 << newpacket->ordernum);
328 free_pages((unsigned long) newpacket->data,
329 newpacket->ordernum);
332 rbu_data.packet_read_count = 0;
333 rbu_data.num_packets = 0;
334 rbu_data.imagesize = 0;
338 * img_update_free: Frees the buffer allocated for storing BIOS image
339 * Always called with lock held and returned with lock held
341 static void img_update_free(void)
343 if (!rbu_data.image_update_buffer)
346 * zero out this buffer before freeing it to get rid of any stale
347 * BIOS image copied in memory.
349 memset(rbu_data.image_update_buffer, 0,
350 rbu_data.image_update_buffer_size);
351 free_pages((unsigned long) rbu_data.image_update_buffer,
352 rbu_data.image_update_ordernum);
355 * Re-initialize the rbu_data variables after a free
357 rbu_data.image_update_ordernum = -1;
358 rbu_data.image_update_buffer = NULL;
359 rbu_data.image_update_buffer_size = 0;
360 rbu_data.bios_image_size = 0;
364 * img_update_realloc: This function allocates the contiguous pages to
365 * accommodate the requested size of data. The memory address and size
366 * values are stored globally and on every call to this function the new
367 * size is checked to see if more data is required than the existing size.
368 * If true the previous memory is freed and new allocation is done to
369 * accommodate the new size. If the incoming size is less then than the
370 * already allocated size, then that memory is reused. This function is
371 * called with lock held and returns with lock held.
373 static int img_update_realloc(unsigned long size)
375 unsigned char *image_update_buffer = NULL;
376 unsigned long img_buf_phys_addr;
380 * check if the buffer of sufficient size has been
383 if (rbu_data.image_update_buffer_size >= size) {
385 * check for corruption
387 if ((size != 0) && (rbu_data.image_update_buffer == NULL)) {
388 pr_err("corruption check failed\n");
392 * we have a valid pre-allocated buffer with
399 * free any previously allocated buffer
403 spin_unlock(&rbu_data.lock);
405 ordernum = get_order(size);
406 image_update_buffer =
407 (unsigned char *)__get_free_pages(GFP_DMA32, ordernum);
408 spin_lock(&rbu_data.lock);
409 if (!image_update_buffer) {
410 pr_debug("Not enough memory for image update: size = %ld\n", size);
414 img_buf_phys_addr = (unsigned long)virt_to_phys(image_update_buffer);
415 if (WARN_ON_ONCE(img_buf_phys_addr > BIOS_SCAN_LIMIT))
416 return -EINVAL; /* can't happen per definition */
418 rbu_data.image_update_buffer = image_update_buffer;
419 rbu_data.image_update_buffer_size = size;
420 rbu_data.bios_image_size = rbu_data.image_update_buffer_size;
421 rbu_data.image_update_ordernum = ordernum;
425 static ssize_t read_packet_data(char *buffer, loff_t pos, size_t count)
430 char *ptempBuf = buffer;
432 /* check to see if we have something to return */
433 if (rbu_data.num_packets == 0) {
434 pr_debug("no packets written\n");
436 goto read_rbu_data_exit;
439 if (pos > rbu_data.imagesize) {
441 pr_warn("data underrun\n");
442 goto read_rbu_data_exit;
445 bytes_left = rbu_data.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) > rbu_data.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 /* check to see if we have something to return */
465 if ((rbu_data.image_update_buffer == NULL) ||
466 (rbu_data.bios_image_size == 0)) {
467 pr_debug("image_update_buffer %p, bios_image_size %lu\n",
468 rbu_data.image_update_buffer,
469 rbu_data.bios_image_size);
473 return memory_read_from_buffer(buffer, count, &pos,
474 rbu_data.image_update_buffer, rbu_data.bios_image_size);
477 static ssize_t data_read(struct file *filp, struct kobject *kobj,
478 struct bin_attribute *bin_attr,
479 char *buffer, loff_t pos, size_t count)
481 ssize_t ret_count = 0;
483 spin_lock(&rbu_data.lock);
485 if (!strcmp(image_type, "mono"))
486 ret_count = read_rbu_mono_data(buffer, pos, count);
487 else if (!strcmp(image_type, "packet"))
488 ret_count = read_packet_data(buffer, pos, count);
490 pr_debug("invalid image type specified\n");
492 spin_unlock(&rbu_data.lock);
495 static BIN_ATTR_RO(data, 0);
497 static void callbackfn_rbu(const struct firmware *fw, void *context)
499 rbu_data.entry_created = 0;
507 spin_lock(&rbu_data.lock);
508 if (!strcmp(image_type, "mono")) {
509 if (!img_update_realloc(fw->size))
510 memcpy(rbu_data.image_update_buffer,
512 } else if (!strcmp(image_type, "packet")) {
514 * we need to free previous packets if a
515 * new hunk of packets needs to be downloaded
518 if (packetize_data(fw->data, fw->size))
519 /* Incase something goes wrong when we are
520 * in middle of packetizing the data, we
521 * need to free up whatever packets might
522 * have been created before we quit.
526 pr_debug("invalid image type specified\n");
527 spin_unlock(&rbu_data.lock);
529 release_firmware(fw);
532 static ssize_t image_type_read(struct file *filp, struct kobject *kobj,
533 struct bin_attribute *bin_attr,
534 char *buffer, loff_t pos, size_t count)
538 size = scnprintf(buffer, count, "%s\n", image_type);
542 static ssize_t image_type_write(struct file *filp, struct kobject *kobj,
543 struct bin_attribute *bin_attr,
544 char *buffer, loff_t pos, size_t count)
549 spin_lock(&rbu_data.lock);
551 * Find the first newline or space
553 for (i = 0; i < count; ++i)
554 if (buffer[i] == '\n' || buffer[i] == ' ') {
559 buffer[count] = '\0';
561 if (strstr(buffer, "mono"))
562 strcpy(image_type, "mono");
563 else if (strstr(buffer, "packet"))
564 strcpy(image_type, "packet");
565 else if (strstr(buffer, "init")) {
567 * If due to the user error the driver gets in a bad
568 * state where even though it is loaded , the
569 * /sys/class/firmware/dell_rbu entries are missing.
570 * to cover this situation the user can recreate entries
571 * by writing init to image_type.
573 if (!rbu_data.entry_created) {
574 spin_unlock(&rbu_data.lock);
575 req_firm_rc = request_firmware_nowait(THIS_MODULE,
576 FW_ACTION_NOUEVENT, "dell_rbu",
577 &rbu_device->dev, GFP_KERNEL, &context,
580 pr_err("request_firmware_nowait failed %d\n", rc);
583 rbu_data.entry_created = 1;
585 spin_lock(&rbu_data.lock);
588 pr_warn("image_type is invalid\n");
589 spin_unlock(&rbu_data.lock);
593 /* we must free all previous allocations */
596 spin_unlock(&rbu_data.lock);
600 static BIN_ATTR_RW(image_type, 0);
602 static ssize_t packet_size_read(struct file *filp, struct kobject *kobj,
603 struct bin_attribute *bin_attr,
604 char *buffer, loff_t pos, size_t count)
608 spin_lock(&rbu_data.lock);
609 size = scnprintf(buffer, count, "%lu\n", rbu_data.packetsize);
610 spin_unlock(&rbu_data.lock);
615 static ssize_t packet_size_write(struct file *filp, struct kobject *kobj,
616 struct bin_attribute *bin_attr,
617 char *buffer, loff_t pos, size_t count)
620 spin_lock(&rbu_data.lock);
622 sscanf(buffer, "%lu", &temp);
623 if (temp < 0xffffffff)
624 rbu_data.packetsize = temp;
626 spin_unlock(&rbu_data.lock);
629 static BIN_ATTR_RW(packet_size, 0);
631 static struct bin_attribute *rbu_bin_attrs[] = {
633 &bin_attr_image_type,
634 &bin_attr_packet_size,
638 static const struct attribute_group rbu_group = {
639 .bin_attrs = rbu_bin_attrs,
642 static int __init dcdrbu_init(void)
645 spin_lock_init(&rbu_data.lock);
648 rbu_device = platform_device_register_simple("dell_rbu", -1, NULL, 0);
649 if (IS_ERR(rbu_device)) {
650 pr_err("platform_device_register_simple failed\n");
651 return PTR_ERR(rbu_device);
654 rc = sysfs_create_group(&rbu_device->dev.kobj, &rbu_group);
658 rbu_data.entry_created = 0;
662 platform_device_unregister(rbu_device);
666 static __exit void dcdrbu_exit(void)
668 spin_lock(&rbu_data.lock);
671 spin_unlock(&rbu_data.lock);
672 sysfs_remove_group(&rbu_device->dev.kobj, &rbu_group);
673 platform_device_unregister(rbu_device);
676 module_exit(dcdrbu_exit);
677 module_init(dcdrbu_init);