📄 dell_rbu.c
字号:
/* * dell_rbu.c * Bios Update driver for Dell systems * Author: Dell Inc * Abhay Salunke <abhay_salunke@dell.com> * * Copyright (C) 2005 Dell Inc. * * Remote BIOS Update (rbu) driver is used for updating DELL BIOS by * creating entries in the /sys file systems on Linux 2.6 and higher * kernels. The driver supports two mechanism to update the BIOS namely * contiguous and packetized. Both these methods still require having some * application to set the CMOS bit indicating the BIOS to update itself * after a reboot. * * Contiguous method: * This driver writes the incoming data in a monolithic image by allocating * contiguous physical pages large enough to accommodate the incoming BIOS * image size. * * Packetized method: * The driver writes the incoming packet image by allocating a new packet * on every time the packet data is written. This driver requires an * application to break the BIOS image in to fixed sized packet chunks. * * See Documentation/dell_rbu.txt for more info. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License v2.0 as published by * the Free Software Foundation * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. */#include <linux/config.h>#include <linux/init.h>#include <linux/module.h>#include <linux/string.h>#include <linux/errno.h>#include <linux/blkdev.h>#include <linux/platform_device.h>#include <linux/spinlock.h>#include <linux/moduleparam.h>#include <linux/firmware.h>#include <linux/dma-mapping.h>MODULE_AUTHOR("Abhay Salunke <abhay_salunke@dell.com>");MODULE_DESCRIPTION("Driver for updating BIOS image on DELL systems");MODULE_LICENSE("GPL");MODULE_VERSION("3.1");#define BIOS_SCAN_LIMIT 0xffffffff#define MAX_IMAGE_LENGTH 16static struct _rbu_data { void *image_update_buffer; unsigned long image_update_buffer_size; unsigned long bios_image_size; int image_update_ordernum; int dma_alloc; spinlock_t lock; unsigned long packet_read_count; unsigned long num_packets; unsigned long packetsize; unsigned long imagesize; int entry_created;} rbu_data;static char image_type[MAX_IMAGE_LENGTH + 1] = "mono";module_param_string(image_type, image_type, sizeof (image_type), 0);MODULE_PARM_DESC(image_type, "BIOS image type. choose- mono or packet or init");static unsigned long allocation_floor = 0x100000;module_param(allocation_floor, ulong, 0644);MODULE_PARM_DESC(allocation_floor, "Minimum address for allocations when using Packet mode");struct packet_data { struct list_head list; size_t length; void *data; int ordernum;};static struct packet_data packet_data_head;static struct platform_device *rbu_device;static int context;static dma_addr_t dell_rbu_dmaaddr;static void init_packet_head(void){ INIT_LIST_HEAD(&packet_data_head.list); rbu_data.packet_read_count = 0; rbu_data.num_packets = 0; rbu_data.packetsize = 0; rbu_data.imagesize = 0;}static int create_packet(void *data, size_t length){ struct packet_data *newpacket; int ordernum = 0; int retval = 0; unsigned int packet_array_size = 0; void **invalid_addr_packet_array = NULL; void *packet_data_temp_buf = NULL; unsigned int idx = 0; pr_debug("create_packet: entry \n"); if (!rbu_data.packetsize) { pr_debug("create_packet: packetsize not specified\n"); retval = -EINVAL; goto out_noalloc; } spin_unlock(&rbu_data.lock); newpacket = kzalloc(sizeof (struct packet_data), GFP_KERNEL); if (!newpacket) { printk(KERN_WARNING "dell_rbu:%s: failed to allocate new " "packet\n", __FUNCTION__); retval = -ENOMEM; spin_lock(&rbu_data.lock); goto out_noalloc; } ordernum = get_order(length); /* * BIOS errata mean we cannot allocate packets below 1MB or they will * be overwritten by BIOS. * * array to temporarily hold packets * that are below the allocation floor * * NOTE: very simplistic because we only need the floor to be at 1MB * due to BIOS errata. This shouldn't be used for higher floors * or you will run out of mem trying to allocate the array. */ packet_array_size = max( (unsigned int)(allocation_floor / rbu_data.packetsize), (unsigned int)1); invalid_addr_packet_array = kzalloc(packet_array_size * sizeof(void*), GFP_KERNEL); if (!invalid_addr_packet_array) { printk(KERN_WARNING "dell_rbu:%s: failed to allocate " "invalid_addr_packet_array \n", __FUNCTION__); retval = -ENOMEM; spin_lock(&rbu_data.lock); goto out_alloc_packet; } while (!packet_data_temp_buf) { packet_data_temp_buf = (unsigned char *) __get_free_pages(GFP_KERNEL, ordernum); if (!packet_data_temp_buf) { printk(KERN_WARNING "dell_rbu:%s: failed to allocate new " "packet\n", __FUNCTION__); retval = -ENOMEM; spin_lock(&rbu_data.lock); goto out_alloc_packet_array; } if ((unsigned long)virt_to_phys(packet_data_temp_buf) < allocation_floor) { pr_debug("packet 0x%lx below floor at 0x%lx.\n", (unsigned long)virt_to_phys( packet_data_temp_buf), allocation_floor); invalid_addr_packet_array[idx++] = packet_data_temp_buf; packet_data_temp_buf = NULL; } } spin_lock(&rbu_data.lock); newpacket->data = packet_data_temp_buf; pr_debug("create_packet: newpacket at physical addr %lx\n", (unsigned long)virt_to_phys(newpacket->data)); /* packets may not have fixed size */ newpacket->length = length; newpacket->ordernum = ordernum; ++rbu_data.num_packets; /* initialize the newly created packet headers */ INIT_LIST_HEAD(&newpacket->list); list_add_tail(&newpacket->list, &packet_data_head.list); memcpy(newpacket->data, data, length); pr_debug("create_packet: exit \n");out_alloc_packet_array: /* always free packet array */ for (;idx>0;idx--) { pr_debug("freeing unused packet below floor 0x%lx.\n", (unsigned long)virt_to_phys( invalid_addr_packet_array[idx-1])); free_pages((unsigned long)invalid_addr_packet_array[idx-1], ordernum); } kfree(invalid_addr_packet_array);out_alloc_packet: /* if error, free data */ if (retval) kfree(newpacket);out_noalloc: return retval;}static int packetize_data(void *data, size_t length){ int rc = 0; int done = 0; int packet_length; u8 *temp; u8 *end = (u8 *) data + length; pr_debug("packetize_data: data length %d\n", length); if (!rbu_data.packetsize) { printk(KERN_WARNING "dell_rbu: packetsize not specified\n"); return -EIO; } temp = (u8 *) data; /* packetize the hunk */ while (!done) { if ((temp + rbu_data.packetsize) < end) packet_length = rbu_data.packetsize; else { /* this is the last packet */ packet_length = end - temp; done = 1; } if ((rc = create_packet(temp, packet_length))) return rc; pr_debug("%lu:%lu\n", temp, (end - temp)); temp += packet_length; } rbu_data.imagesize = length; return rc;}static int do_packet_read(char *data, struct list_head *ptemp_list, int length, int bytes_read, int *list_read_count){ void *ptemp_buf; struct packet_data *newpacket = NULL; int bytes_copied = 0; int j = 0; newpacket = list_entry(ptemp_list, struct packet_data, list); *list_read_count += newpacket->length; if (*list_read_count > bytes_read) { /* point to the start of unread data */ j = newpacket->length - (*list_read_count - bytes_read); /* point to the offset in the packet buffer */ ptemp_buf = (u8 *) newpacket->data + j; /* * check if there is enough room in * * the incoming buffer */ if (length > (*list_read_count - bytes_read)) /* * copy what ever is there in this * packet and move on */ bytes_copied = (*list_read_count - bytes_read); else /* copy the remaining */ bytes_copied = length; memcpy(data, ptemp_buf, bytes_copied); } return bytes_copied;}static int packet_read_list(char *data, size_t * pread_length){ struct list_head *ptemp_list; int temp_count = 0; int bytes_copied = 0; int bytes_read = 0; int remaining_bytes = 0; char *pdest = data; /* check if we have any packets */ if (0 == rbu_data.num_packets) return -ENOMEM; remaining_bytes = *pread_length; bytes_read = rbu_data.packet_read_count; ptemp_list = (&packet_data_head.list)->next; while (!list_empty(ptemp_list)) { bytes_copied = do_packet_read(pdest, ptemp_list, remaining_bytes, bytes_read, &temp_count); remaining_bytes -= bytes_copied; bytes_read += bytes_copied; pdest += bytes_copied; /* * check if we reached end of buffer before reaching the * last packet */ if (remaining_bytes == 0) break; ptemp_list = ptemp_list->next; } /*finally set the bytes read */ *pread_length = bytes_read - rbu_data.packet_read_count; rbu_data.packet_read_count = bytes_read; return 0;}static void packet_empty_list(void){ struct list_head *ptemp_list; struct list_head *pnext_list; struct packet_data *newpacket; ptemp_list = (&packet_data_head.list)->next; while (!list_empty(ptemp_list)) { newpacket = list_entry(ptemp_list, struct packet_data, list); pnext_list = ptemp_list->next; list_del(ptemp_list); ptemp_list = pnext_list; /* * zero out the RBU packet memory before freeing * to make sure there are no stale RBU packets left in memory */ memset(newpacket->data, 0, rbu_data.packetsize); free_pages((unsigned long) newpacket->data, newpacket->ordernum); kfree(newpacket); } rbu_data.packet_read_count = 0; rbu_data.num_packets = 0; rbu_data.imagesize = 0;}/* * img_update_free: Frees the buffer allocated for storing BIOS image * Always called with lock held and returned with lock held */static void img_update_free(void){ if (!rbu_data.image_update_buffer) return; /* * zero out this buffer before freeing it to get rid of any stale * BIOS image copied in memory. */ memset(rbu_data.image_update_buffer, 0, rbu_data.image_update_buffer_size); if (rbu_data.dma_alloc == 1) dma_free_coherent(NULL, rbu_data.bios_image_size, rbu_data.image_update_buffer, dell_rbu_dmaaddr); else free_pages((unsigned long) rbu_data.image_update_buffer, rbu_data.image_update_ordernum); /*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -