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.
29 #include <linux/init.h>
30 #include <linux/module.h>
31 #include <linux/slab.h>
32 #include <linux/string.h>
33 #include <linux/errno.h>
34 #include <linux/blkdev.h>
35 #include <linux/platform_device.h>
36 #include <linux/spinlock.h>
37 #include <linux/moduleparam.h>
38 #include <linux/firmware.h>
39 #include <linux/dma-mapping.h>
40 #include <asm/set_memory.h>
42 MODULE_AUTHOR("Abhay Salunke <abhay_salunke@dell.com>");
43 MODULE_DESCRIPTION("Driver for updating BIOS image on DELL systems");
44 MODULE_LICENSE("GPL");
45 MODULE_VERSION("3.2");
47 #define BIOS_SCAN_LIMIT 0xffffffff
48 #define MAX_IMAGE_LENGTH 16
49 static struct _rbu_data
{
50 void *image_update_buffer
;
51 unsigned long image_update_buffer_size
;
52 unsigned long bios_image_size
;
53 int image_update_ordernum
;
55 unsigned long packet_read_count
;
56 unsigned long num_packets
;
57 unsigned long packetsize
;
58 unsigned long imagesize
;
62 static char image_type
[MAX_IMAGE_LENGTH
+ 1] = "mono";
63 module_param_string(image_type
, image_type
, sizeof (image_type
), 0);
64 MODULE_PARM_DESC(image_type
,
65 "BIOS image type. choose- mono or packet or init");
67 static unsigned long allocation_floor
= 0x100000;
68 module_param(allocation_floor
, ulong
, 0644);
69 MODULE_PARM_DESC(allocation_floor
,
70 "Minimum address for allocations when using Packet mode");
73 struct list_head list
;
79 static struct packet_data packet_data_head
;
81 static struct platform_device
*rbu_device
;
84 static void init_packet_head(void)
86 INIT_LIST_HEAD(&packet_data_head
.list
);
87 rbu_data
.packet_read_count
= 0;
88 rbu_data
.num_packets
= 0;
89 rbu_data
.packetsize
= 0;
90 rbu_data
.imagesize
= 0;
93 static int create_packet(void *data
, size_t length
)
95 struct packet_data
*newpacket
;
98 unsigned int packet_array_size
= 0;
99 void **invalid_addr_packet_array
= NULL
;
100 void *packet_data_temp_buf
= NULL
;
101 unsigned int idx
= 0;
103 pr_debug("create_packet: entry \n");
105 if (!rbu_data
.packetsize
) {
106 pr_debug("create_packet: packetsize not specified\n");
111 spin_unlock(&rbu_data
.lock
);
113 newpacket
= kzalloc(sizeof (struct packet_data
), GFP_KERNEL
);
117 "dell_rbu:%s: failed to allocate new "
118 "packet\n", __func__
);
120 spin_lock(&rbu_data
.lock
);
124 ordernum
= get_order(length
);
127 * BIOS errata mean we cannot allocate packets below 1MB or they will
128 * be overwritten by BIOS.
130 * array to temporarily hold packets
131 * that are below the allocation floor
133 * NOTE: very simplistic because we only need the floor to be at 1MB
134 * due to BIOS errata. This shouldn't be used for higher floors
135 * or you will run out of mem trying to allocate the array.
137 packet_array_size
= max(
138 (unsigned int)(allocation_floor
/ rbu_data
.packetsize
),
140 invalid_addr_packet_array
= kcalloc(packet_array_size
, sizeof(void *),
143 if (!invalid_addr_packet_array
) {
145 "dell_rbu:%s: failed to allocate "
146 "invalid_addr_packet_array \n",
149 spin_lock(&rbu_data
.lock
);
150 goto out_alloc_packet
;
153 while (!packet_data_temp_buf
) {
154 packet_data_temp_buf
= (unsigned char *)
155 __get_free_pages(GFP_KERNEL
, ordernum
);
156 if (!packet_data_temp_buf
) {
158 "dell_rbu:%s: failed to allocate new "
159 "packet\n", __func__
);
161 spin_lock(&rbu_data
.lock
);
162 goto out_alloc_packet_array
;
165 if ((unsigned long)virt_to_phys(packet_data_temp_buf
)
166 < allocation_floor
) {
167 pr_debug("packet 0x%lx below floor at 0x%lx.\n",
168 (unsigned long)virt_to_phys(
169 packet_data_temp_buf
),
171 invalid_addr_packet_array
[idx
++] = packet_data_temp_buf
;
172 packet_data_temp_buf
= NULL
;
176 * set to uncachable or it may never get written back before reboot
178 set_memory_uc((unsigned long)packet_data_temp_buf
, 1 << ordernum
);
180 spin_lock(&rbu_data
.lock
);
182 newpacket
->data
= packet_data_temp_buf
;
184 pr_debug("create_packet: newpacket at physical addr %lx\n",
185 (unsigned long)virt_to_phys(newpacket
->data
));
187 /* packets may not have fixed size */
188 newpacket
->length
= length
;
189 newpacket
->ordernum
= ordernum
;
190 ++rbu_data
.num_packets
;
192 /* initialize the newly created packet headers */
193 INIT_LIST_HEAD(&newpacket
->list
);
194 list_add_tail(&newpacket
->list
, &packet_data_head
.list
);
196 memcpy(newpacket
->data
, data
, length
);
198 pr_debug("create_packet: exit \n");
200 out_alloc_packet_array
:
201 /* always free packet array */
203 pr_debug("freeing unused packet below floor 0x%lx.\n",
204 (unsigned long)virt_to_phys(
205 invalid_addr_packet_array
[idx
-1]));
206 free_pages((unsigned long)invalid_addr_packet_array
[idx
-1],
209 kfree(invalid_addr_packet_array
);
212 /* if error, free data */
220 static int packetize_data(const u8
*data
, size_t length
)
226 u8
*end
= (u8
*) data
+ length
;
227 pr_debug("packetize_data: data length %zd\n", length
);
228 if (!rbu_data
.packetsize
) {
230 "dell_rbu: packetsize not specified\n");
236 /* packetize the hunk */
238 if ((temp
+ rbu_data
.packetsize
) < end
)
239 packet_length
= rbu_data
.packetsize
;
241 /* this is the last packet */
242 packet_length
= end
- temp
;
246 if ((rc
= create_packet(temp
, packet_length
)))
249 pr_debug("%p:%td\n", temp
, (end
- temp
));
250 temp
+= packet_length
;
253 rbu_data
.imagesize
= length
;
258 static int do_packet_read(char *data
, struct list_head
*ptemp_list
,
259 int length
, int bytes_read
, int *list_read_count
)
262 struct packet_data
*newpacket
= NULL
;
263 int bytes_copied
= 0;
266 newpacket
= list_entry(ptemp_list
, struct packet_data
, list
);
267 *list_read_count
+= newpacket
->length
;
269 if (*list_read_count
> bytes_read
) {
270 /* point to the start of unread data */
271 j
= newpacket
->length
- (*list_read_count
- bytes_read
);
272 /* point to the offset in the packet buffer */
273 ptemp_buf
= (u8
*) newpacket
->data
+ j
;
275 * check if there is enough room in
276 * * the incoming buffer
278 if (length
> (*list_read_count
- bytes_read
))
280 * copy what ever is there in this
283 bytes_copied
= (*list_read_count
- bytes_read
);
285 /* copy the remaining */
286 bytes_copied
= length
;
287 memcpy(data
, ptemp_buf
, bytes_copied
);
292 static int packet_read_list(char *data
, size_t * pread_length
)
294 struct list_head
*ptemp_list
;
296 int bytes_copied
= 0;
298 int remaining_bytes
= 0;
301 /* check if we have any packets */
302 if (0 == rbu_data
.num_packets
)
305 remaining_bytes
= *pread_length
;
306 bytes_read
= rbu_data
.packet_read_count
;
308 ptemp_list
= (&packet_data_head
.list
)->next
;
309 while (!list_empty(ptemp_list
)) {
310 bytes_copied
= do_packet_read(pdest
, ptemp_list
,
311 remaining_bytes
, bytes_read
, &temp_count
);
312 remaining_bytes
-= bytes_copied
;
313 bytes_read
+= bytes_copied
;
314 pdest
+= bytes_copied
;
316 * check if we reached end of buffer before reaching the
319 if (remaining_bytes
== 0)
322 ptemp_list
= ptemp_list
->next
;
324 /*finally set the bytes read */
325 *pread_length
= bytes_read
- rbu_data
.packet_read_count
;
326 rbu_data
.packet_read_count
= bytes_read
;
330 static void packet_empty_list(void)
332 struct list_head
*ptemp_list
;
333 struct list_head
*pnext_list
;
334 struct packet_data
*newpacket
;
336 ptemp_list
= (&packet_data_head
.list
)->next
;
337 while (!list_empty(ptemp_list
)) {
339 list_entry(ptemp_list
, struct packet_data
, list
);
340 pnext_list
= ptemp_list
->next
;
341 list_del(ptemp_list
);
342 ptemp_list
= pnext_list
;
344 * zero out the RBU packet memory before freeing
345 * to make sure there are no stale RBU packets left in memory
347 memset(newpacket
->data
, 0, rbu_data
.packetsize
);
348 set_memory_wb((unsigned long)newpacket
->data
,
349 1 << newpacket
->ordernum
);
350 free_pages((unsigned long) newpacket
->data
,
351 newpacket
->ordernum
);
354 rbu_data
.packet_read_count
= 0;
355 rbu_data
.num_packets
= 0;
356 rbu_data
.imagesize
= 0;
360 * img_update_free: Frees the buffer allocated for storing BIOS image
361 * Always called with lock held and returned with lock held
363 static void img_update_free(void)
365 if (!rbu_data
.image_update_buffer
)
368 * zero out this buffer before freeing it to get rid of any stale
369 * BIOS image copied in memory.
371 memset(rbu_data
.image_update_buffer
, 0,
372 rbu_data
.image_update_buffer_size
);
373 free_pages((unsigned long) rbu_data
.image_update_buffer
,
374 rbu_data
.image_update_ordernum
);
377 * Re-initialize the rbu_data variables after a free
379 rbu_data
.image_update_ordernum
= -1;
380 rbu_data
.image_update_buffer
= NULL
;
381 rbu_data
.image_update_buffer_size
= 0;
382 rbu_data
.bios_image_size
= 0;
386 * img_update_realloc: This function allocates the contiguous pages to
387 * accommodate the requested size of data. The memory address and size
388 * values are stored globally and on every call to this function the new
389 * size is checked to see if more data is required than the existing size.
390 * If true the previous memory is freed and new allocation is done to
391 * accommodate the new size. If the incoming size is less then than the
392 * already allocated size, then that memory is reused. This function is
393 * called with lock held and returns with lock held.
395 static int img_update_realloc(unsigned long size
)
397 unsigned char *image_update_buffer
= NULL
;
398 unsigned long img_buf_phys_addr
;
402 * check if the buffer of sufficient size has been
405 if (rbu_data
.image_update_buffer_size
>= size
) {
407 * check for corruption
409 if ((size
!= 0) && (rbu_data
.image_update_buffer
== NULL
)) {
410 printk(KERN_ERR
"dell_rbu:%s: corruption "
411 "check failed\n", __func__
);
415 * we have a valid pre-allocated buffer with
422 * free any previously allocated buffer
426 spin_unlock(&rbu_data
.lock
);
428 ordernum
= get_order(size
);
429 image_update_buffer
=
430 (unsigned char *)__get_free_pages(GFP_DMA32
, ordernum
);
431 spin_lock(&rbu_data
.lock
);
432 if (!image_update_buffer
) {
433 pr_debug("Not enough memory for image update:"
434 "size = %ld\n", size
);
438 img_buf_phys_addr
= (unsigned long)virt_to_phys(image_update_buffer
);
439 if (WARN_ON_ONCE(img_buf_phys_addr
> BIOS_SCAN_LIMIT
))
440 return -EINVAL
; /* can't happen per definition */
442 rbu_data
.image_update_buffer
= image_update_buffer
;
443 rbu_data
.image_update_buffer_size
= size
;
444 rbu_data
.bios_image_size
= rbu_data
.image_update_buffer_size
;
445 rbu_data
.image_update_ordernum
= ordernum
;
449 static ssize_t
read_packet_data(char *buffer
, loff_t pos
, size_t count
)
454 char *ptempBuf
= buffer
;
456 /* check to see if we have something to return */
457 if (rbu_data
.num_packets
== 0) {
458 pr_debug("read_packet_data: no packets written\n");
460 goto read_rbu_data_exit
;
463 if (pos
> rbu_data
.imagesize
) {
465 printk(KERN_WARNING
"dell_rbu:read_packet_data: "
467 goto read_rbu_data_exit
;
470 bytes_left
= rbu_data
.imagesize
- pos
;
471 data_length
= min(bytes_left
, count
);
473 if ((retval
= packet_read_list(ptempBuf
, &data_length
)) < 0)
474 goto read_rbu_data_exit
;
476 if ((pos
+ count
) > rbu_data
.imagesize
) {
477 rbu_data
.packet_read_count
= 0;
478 /* this was the last copy */
487 static ssize_t
read_rbu_mono_data(char *buffer
, loff_t pos
, size_t count
)
489 /* check to see if we have something to return */
490 if ((rbu_data
.image_update_buffer
== NULL
) ||
491 (rbu_data
.bios_image_size
== 0)) {
492 pr_debug("read_rbu_data_mono: image_update_buffer %p ,"
493 "bios_image_size %lu\n",
494 rbu_data
.image_update_buffer
,
495 rbu_data
.bios_image_size
);
499 return memory_read_from_buffer(buffer
, count
, &pos
,
500 rbu_data
.image_update_buffer
, rbu_data
.bios_image_size
);
503 static ssize_t
read_rbu_data(struct file
*filp
, struct kobject
*kobj
,
504 struct bin_attribute
*bin_attr
,
505 char *buffer
, loff_t pos
, size_t count
)
507 ssize_t ret_count
= 0;
509 spin_lock(&rbu_data
.lock
);
511 if (!strcmp(image_type
, "mono"))
512 ret_count
= read_rbu_mono_data(buffer
, pos
, count
);
513 else if (!strcmp(image_type
, "packet"))
514 ret_count
= read_packet_data(buffer
, pos
, count
);
516 pr_debug("read_rbu_data: invalid image type specified\n");
518 spin_unlock(&rbu_data
.lock
);
522 static void callbackfn_rbu(const struct firmware
*fw
, void *context
)
524 rbu_data
.entry_created
= 0;
532 spin_lock(&rbu_data
.lock
);
533 if (!strcmp(image_type
, "mono")) {
534 if (!img_update_realloc(fw
->size
))
535 memcpy(rbu_data
.image_update_buffer
,
537 } else if (!strcmp(image_type
, "packet")) {
539 * we need to free previous packets if a
540 * new hunk of packets needs to be downloaded
543 if (packetize_data(fw
->data
, fw
->size
))
544 /* Incase something goes wrong when we are
545 * in middle of packetizing the data, we
546 * need to free up whatever packets might
547 * have been created before we quit.
551 pr_debug("invalid image type specified.\n");
552 spin_unlock(&rbu_data
.lock
);
554 release_firmware(fw
);
557 static ssize_t
read_rbu_image_type(struct file
*filp
, struct kobject
*kobj
,
558 struct bin_attribute
*bin_attr
,
559 char *buffer
, loff_t pos
, size_t count
)
563 size
= scnprintf(buffer
, count
, "%s\n", image_type
);
567 static ssize_t
write_rbu_image_type(struct file
*filp
, struct kobject
*kobj
,
568 struct bin_attribute
*bin_attr
,
569 char *buffer
, loff_t pos
, size_t count
)
574 spin_lock(&rbu_data
.lock
);
576 * Find the first newline or space
578 for (i
= 0; i
< count
; ++i
)
579 if (buffer
[i
] == '\n' || buffer
[i
] == ' ') {
584 buffer
[count
] = '\0';
586 if (strstr(buffer
, "mono"))
587 strcpy(image_type
, "mono");
588 else if (strstr(buffer
, "packet"))
589 strcpy(image_type
, "packet");
590 else if (strstr(buffer
, "init")) {
592 * If due to the user error the driver gets in a bad
593 * state where even though it is loaded , the
594 * /sys/class/firmware/dell_rbu entries are missing.
595 * to cover this situation the user can recreate entries
596 * by writing init to image_type.
598 if (!rbu_data
.entry_created
) {
599 spin_unlock(&rbu_data
.lock
);
600 req_firm_rc
= request_firmware_nowait(THIS_MODULE
,
601 FW_ACTION_NOHOTPLUG
, "dell_rbu",
602 &rbu_device
->dev
, GFP_KERNEL
, &context
,
606 "dell_rbu:%s request_firmware_nowait"
607 " failed %d\n", __func__
, rc
);
610 rbu_data
.entry_created
= 1;
612 spin_lock(&rbu_data
.lock
);
615 printk(KERN_WARNING
"dell_rbu: image_type is invalid\n");
616 spin_unlock(&rbu_data
.lock
);
620 /* we must free all previous allocations */
623 spin_unlock(&rbu_data
.lock
);
628 static ssize_t
read_rbu_packet_size(struct file
*filp
, struct kobject
*kobj
,
629 struct bin_attribute
*bin_attr
,
630 char *buffer
, loff_t pos
, size_t count
)
634 spin_lock(&rbu_data
.lock
);
635 size
= scnprintf(buffer
, count
, "%lu\n", rbu_data
.packetsize
);
636 spin_unlock(&rbu_data
.lock
);
641 static ssize_t
write_rbu_packet_size(struct file
*filp
, struct kobject
*kobj
,
642 struct bin_attribute
*bin_attr
,
643 char *buffer
, loff_t pos
, size_t count
)
646 spin_lock(&rbu_data
.lock
);
648 sscanf(buffer
, "%lu", &temp
);
649 if (temp
< 0xffffffff)
650 rbu_data
.packetsize
= temp
;
652 spin_unlock(&rbu_data
.lock
);
656 static struct bin_attribute rbu_data_attr
= {
657 .attr
= {.name
= "data", .mode
= 0444},
658 .read
= read_rbu_data
,
661 static struct bin_attribute rbu_image_type_attr
= {
662 .attr
= {.name
= "image_type", .mode
= 0644},
663 .read
= read_rbu_image_type
,
664 .write
= write_rbu_image_type
,
667 static struct bin_attribute rbu_packet_size_attr
= {
668 .attr
= {.name
= "packet_size", .mode
= 0644},
669 .read
= read_rbu_packet_size
,
670 .write
= write_rbu_packet_size
,
673 static int __init
dcdrbu_init(void)
676 spin_lock_init(&rbu_data
.lock
);
679 rbu_device
= platform_device_register_simple("dell_rbu", -1, NULL
, 0);
680 if (IS_ERR(rbu_device
)) {
682 "dell_rbu:%s:platform_device_register_simple "
683 "failed\n", __func__
);
684 return PTR_ERR(rbu_device
);
687 rc
= sysfs_create_bin_file(&rbu_device
->dev
.kobj
, &rbu_data_attr
);
690 rc
= sysfs_create_bin_file(&rbu_device
->dev
.kobj
, &rbu_image_type_attr
);
693 rc
= sysfs_create_bin_file(&rbu_device
->dev
.kobj
,
694 &rbu_packet_size_attr
);
698 rbu_data
.entry_created
= 0;
702 sysfs_remove_bin_file(&rbu_device
->dev
.kobj
, &rbu_image_type_attr
);
704 sysfs_remove_bin_file(&rbu_device
->dev
.kobj
, &rbu_data_attr
);
706 platform_device_unregister(rbu_device
);
710 static __exit
void dcdrbu_exit(void)
712 spin_lock(&rbu_data
.lock
);
715 spin_unlock(&rbu_data
.lock
);
716 platform_device_unregister(rbu_device
);
719 module_exit(dcdrbu_exit
);
720 module_init(dcdrbu_init
);
722 /* vim:noet:ts=8:sw=8