dell_rbu.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. /*
  2. * dell_rbu.c
  3. * Bios Update driver for Dell systems
  4. * Author: Dell Inc
  5. * Abhay Salunke <abhay_salunke@dell.com>
  6. *
  7. * Copyright (C) 2005 Dell Inc.
  8. *
  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
  14. * after a reboot.
  15. *
  16. * Contiguous method:
  17. * This driver writes the incoming data in a monolithic image by allocating
  18. * contiguous physical pages large enough to accommodate the incoming BIOS
  19. * image size.
  20. *
  21. * Packetized method:
  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.
  25. *
  26. * See Documentation/dell_rbu.txt for more info.
  27. *
  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
  31. *
  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.
  36. */
  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>
  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("1.0");
  53. #define BIOS_SCAN_LIMIT 0xffffffff
  54. #define MAX_IMAGE_LENGTH 16
  55. static struct _rbu_data {
  56. void *image_update_buffer;
  57. unsigned long image_update_buffer_size;
  58. unsigned long bios_image_size;
  59. int image_update_ordernum;
  60. int dma_alloc;
  61. spinlock_t lock;
  62. unsigned long packet_read_count;
  63. unsigned long packet_write_count;
  64. unsigned long num_packets;
  65. unsigned long packetsize;
  66. } rbu_data;
  67. static char image_type[MAX_IMAGE_LENGTH] = "mono";
  68. module_param_string(image_type, image_type, sizeof(image_type), 0);
  69. MODULE_PARM_DESC(image_type, "BIOS image type. choose- mono or packet");
  70. struct packet_data {
  71. struct list_head list;
  72. size_t length;
  73. void *data;
  74. int ordernum;
  75. };
  76. static struct packet_data packet_data_head;
  77. static struct platform_device *rbu_device;
  78. static int context;
  79. static dma_addr_t dell_rbu_dmaaddr;
  80. static void init_packet_head(void)
  81. {
  82. INIT_LIST_HEAD(&packet_data_head.list);
  83. rbu_data.packet_write_count = 0;
  84. rbu_data.packet_read_count = 0;
  85. rbu_data.num_packets = 0;
  86. rbu_data.packetsize = 0;
  87. }
  88. static int fill_last_packet(void *data, size_t length)
  89. {
  90. struct list_head *ptemp_list;
  91. struct packet_data *packet = NULL;
  92. int packet_count = 0;
  93. pr_debug("fill_last_packet: entry \n");
  94. if (!rbu_data.num_packets) {
  95. pr_debug("fill_last_packet: num_packets=0\n");
  96. return -ENOMEM;
  97. }
  98. packet_count = rbu_data.num_packets;
  99. ptemp_list = (&packet_data_head.list)->prev;
  100. packet = list_entry(ptemp_list, struct packet_data, list);
  101. if ((rbu_data.packet_write_count + length) > rbu_data.packetsize) {
  102. pr_debug("dell_rbu:%s: packet size data "
  103. "overrun\n", __FUNCTION__);
  104. return -EINVAL;
  105. }
  106. pr_debug("fill_last_packet : buffer = %p\n", packet->data);
  107. memcpy((packet->data + rbu_data.packet_write_count), data, length);
  108. if ((rbu_data.packet_write_count + length) == rbu_data.packetsize) {
  109. /*
  110. * this was the last data chunk in the packet
  111. * so reinitialize the packet data counter to zero
  112. */
  113. rbu_data.packet_write_count = 0;
  114. } else
  115. rbu_data.packet_write_count += length;
  116. pr_debug("fill_last_packet: exit \n");
  117. return 0;
  118. }
  119. static int create_packet(size_t length)
  120. {
  121. struct packet_data *newpacket;
  122. int ordernum = 0;
  123. pr_debug("create_packet: entry \n");
  124. if (!rbu_data.packetsize) {
  125. pr_debug("create_packet: packetsize not specified\n");
  126. return -EINVAL;
  127. }
  128. newpacket = kmalloc(sizeof(struct packet_data), GFP_KERNEL);
  129. if (!newpacket) {
  130. printk(KERN_WARNING
  131. "dell_rbu:%s: failed to allocate new "
  132. "packet\n", __FUNCTION__);
  133. return -ENOMEM;
  134. }
  135. ordernum = get_order(length);
  136. /*
  137. * there is no upper limit on memory
  138. * address for packetized mechanism
  139. */
  140. newpacket->data = (unsigned char *)__get_free_pages(GFP_KERNEL,
  141. ordernum);
  142. pr_debug("create_packet: newpacket %p\n", newpacket->data);
  143. if (!newpacket->data) {
  144. printk(KERN_WARNING
  145. "dell_rbu:%s: failed to allocate new "
  146. "packet\n", __FUNCTION__);
  147. kfree(newpacket);
  148. return -ENOMEM;
  149. }
  150. newpacket->ordernum = ordernum;
  151. ++rbu_data.num_packets;
  152. /*
  153. * initialize the newly created packet headers
  154. */
  155. INIT_LIST_HEAD(&newpacket->list);
  156. list_add_tail(&newpacket->list, &packet_data_head.list);
  157. /*
  158. * packets have fixed size
  159. */
  160. newpacket->length = rbu_data.packetsize;
  161. pr_debug("create_packet: exit \n");
  162. return 0;
  163. }
  164. static int packetize_data(void *data, size_t length)
  165. {
  166. int rc = 0;
  167. if (!rbu_data.packet_write_count) {
  168. if ((rc = create_packet(length)))
  169. return rc;
  170. }
  171. if ((rc = fill_last_packet(data, length)))
  172. return rc;
  173. return rc;
  174. }
  175. static int
  176. do_packet_read(char *data, struct list_head *ptemp_list,
  177. int length, int bytes_read, int *list_read_count)
  178. {
  179. void *ptemp_buf;
  180. struct packet_data *newpacket = NULL;
  181. int bytes_copied = 0;
  182. int j = 0;
  183. newpacket = list_entry(ptemp_list, struct packet_data, list);
  184. *list_read_count += newpacket->length;
  185. if (*list_read_count > bytes_read) {
  186. /* point to the start of unread data */
  187. j = newpacket->length - (*list_read_count - bytes_read);
  188. /* point to the offset in the packet buffer */
  189. ptemp_buf = (u8 *) newpacket->data + j;
  190. /*
  191. * check if there is enough room in
  192. * * the incoming buffer
  193. */
  194. if (length > (*list_read_count - bytes_read))
  195. /*
  196. * copy what ever is there in this
  197. * packet and move on
  198. */
  199. bytes_copied = (*list_read_count - bytes_read);
  200. else
  201. /* copy the remaining */
  202. bytes_copied = length;
  203. memcpy(data, ptemp_buf, bytes_copied);
  204. }
  205. return bytes_copied;
  206. }
  207. static int packet_read_list(char *data, size_t * pread_length)
  208. {
  209. struct list_head *ptemp_list;
  210. int temp_count = 0;
  211. int bytes_copied = 0;
  212. int bytes_read = 0;
  213. int remaining_bytes = 0;
  214. char *pdest = data;
  215. /* check if we have any packets */
  216. if (0 == rbu_data.num_packets)
  217. return -ENOMEM;
  218. remaining_bytes = *pread_length;
  219. bytes_read = rbu_data.packet_read_count;
  220. ptemp_list = (&packet_data_head.list)->next;
  221. while (!list_empty(ptemp_list)) {
  222. bytes_copied = do_packet_read(pdest, ptemp_list,
  223. remaining_bytes, bytes_read,
  224. &temp_count);
  225. remaining_bytes -= bytes_copied;
  226. bytes_read += bytes_copied;
  227. pdest += bytes_copied;
  228. /*
  229. * check if we reached end of buffer before reaching the
  230. * last packet
  231. */
  232. if (remaining_bytes == 0)
  233. break;
  234. ptemp_list = ptemp_list->next;
  235. }
  236. /*finally set the bytes read */
  237. *pread_length = bytes_read - rbu_data.packet_read_count;
  238. rbu_data.packet_read_count = bytes_read;
  239. return 0;
  240. }
  241. static void packet_empty_list(void)
  242. {
  243. struct list_head *ptemp_list;
  244. struct list_head *pnext_list;
  245. struct packet_data *newpacket;
  246. ptemp_list = (&packet_data_head.list)->next;
  247. while (!list_empty(ptemp_list)) {
  248. newpacket =
  249. list_entry(ptemp_list, struct packet_data, list);
  250. pnext_list = ptemp_list->next;
  251. list_del(ptemp_list);
  252. ptemp_list = pnext_list;
  253. /*
  254. * zero out the RBU packet memory before freeing
  255. * to make sure there are no stale RBU packets left in memory
  256. */
  257. memset(newpacket->data, 0, rbu_data.packetsize);
  258. free_pages((unsigned long)newpacket->data,
  259. newpacket->ordernum);
  260. kfree(newpacket);
  261. }
  262. rbu_data.packet_write_count = 0;
  263. rbu_data.packet_read_count = 0;
  264. rbu_data.num_packets = 0;
  265. rbu_data.packetsize = 0;
  266. }
  267. /*
  268. * img_update_free: Frees the buffer allocated for storing BIOS image
  269. * Always called with lock held and returned with lock held
  270. */
  271. static void img_update_free(void)
  272. {
  273. if (!rbu_data.image_update_buffer)
  274. return;
  275. /*
  276. * zero out this buffer before freeing it to get rid of any stale
  277. * BIOS image copied in memory.
  278. */
  279. memset(rbu_data.image_update_buffer, 0,
  280. rbu_data.image_update_buffer_size);
  281. if (rbu_data.dma_alloc == 1)
  282. dma_free_coherent(NULL, rbu_data.bios_image_size,
  283. rbu_data.image_update_buffer,
  284. dell_rbu_dmaaddr);
  285. else
  286. free_pages((unsigned long)rbu_data.image_update_buffer,
  287. rbu_data.image_update_ordernum);
  288. /*
  289. * Re-initialize the rbu_data variables after a free
  290. */
  291. rbu_data.image_update_ordernum = -1;
  292. rbu_data.image_update_buffer = NULL;
  293. rbu_data.image_update_buffer_size = 0;
  294. rbu_data.bios_image_size = 0;
  295. rbu_data.dma_alloc = 0;
  296. }
  297. /*
  298. * img_update_realloc: This function allocates the contiguous pages to
  299. * accommodate the requested size of data. The memory address and size
  300. * values are stored globally and on every call to this function the new
  301. * size is checked to see if more data is required than the existing size.
  302. * If true the previous memory is freed and new allocation is done to
  303. * accommodate the new size. If the incoming size is less then than the
  304. * already allocated size, then that memory is reused. This function is
  305. * called with lock held and returns with lock held.
  306. */
  307. static int img_update_realloc(unsigned long size)
  308. {
  309. unsigned char *image_update_buffer = NULL;
  310. unsigned long rc;
  311. unsigned long img_buf_phys_addr;
  312. int ordernum;
  313. int dma_alloc = 0;
  314. /*
  315. * check if the buffer of sufficient size has been
  316. * already allocated
  317. */
  318. if (rbu_data.image_update_buffer_size >= size) {
  319. /*
  320. * check for corruption
  321. */
  322. if ((size != 0) && (rbu_data.image_update_buffer == NULL)) {
  323. printk(KERN_ERR "dell_rbu:%s: corruption "
  324. "check failed\n", __FUNCTION__);
  325. return -EINVAL;
  326. }
  327. /*
  328. * we have a valid pre-allocated buffer with
  329. * sufficient size
  330. */
  331. return 0;
  332. }
  333. /*
  334. * free any previously allocated buffer
  335. */
  336. img_update_free();
  337. spin_unlock(&rbu_data.lock);
  338. ordernum = get_order(size);
  339. image_update_buffer =
  340. (unsigned char *)__get_free_pages(GFP_KERNEL, ordernum);
  341. img_buf_phys_addr =
  342. (unsigned long)virt_to_phys(image_update_buffer);
  343. if (img_buf_phys_addr > BIOS_SCAN_LIMIT) {
  344. free_pages((unsigned long)image_update_buffer, ordernum);
  345. ordernum = -1;
  346. image_update_buffer = dma_alloc_coherent(NULL, size,
  347. &dell_rbu_dmaaddr,
  348. GFP_KERNEL);
  349. dma_alloc = 1;
  350. }
  351. spin_lock(&rbu_data.lock);
  352. if (image_update_buffer != NULL) {
  353. rbu_data.image_update_buffer = image_update_buffer;
  354. rbu_data.image_update_buffer_size = size;
  355. rbu_data.bios_image_size =
  356. rbu_data.image_update_buffer_size;
  357. rbu_data.image_update_ordernum = ordernum;
  358. rbu_data.dma_alloc = dma_alloc;
  359. rc = 0;
  360. } else {
  361. pr_debug("Not enough memory for image update:"
  362. "size = %ld\n", size);
  363. rc = -ENOMEM;
  364. }
  365. return rc;
  366. }
  367. static ssize_t read_packet_data(char *buffer, loff_t pos, size_t count)
  368. {
  369. int retval;
  370. size_t bytes_left;
  371. size_t data_length;
  372. char *ptempBuf = buffer;
  373. unsigned long imagesize;
  374. /* check to see if we have something to return */
  375. if (rbu_data.num_packets == 0) {
  376. pr_debug("read_packet_data: no packets written\n");
  377. retval = -ENOMEM;
  378. goto read_rbu_data_exit;
  379. }
  380. imagesize = rbu_data.num_packets * rbu_data.packetsize;
  381. if (pos > imagesize) {
  382. retval = 0;
  383. printk(KERN_WARNING "dell_rbu:read_packet_data: "
  384. "data underrun\n");
  385. goto read_rbu_data_exit;
  386. }
  387. bytes_left = imagesize - pos;
  388. data_length = min(bytes_left, count);
  389. if ((retval = packet_read_list(ptempBuf, &data_length)) < 0)
  390. goto read_rbu_data_exit;
  391. if ((pos + count) > imagesize) {
  392. rbu_data.packet_read_count = 0;
  393. /* this was the last copy */
  394. retval = bytes_left;
  395. } else
  396. retval = count;
  397. read_rbu_data_exit:
  398. return retval;
  399. }
  400. static ssize_t read_rbu_mono_data(char *buffer, loff_t pos, size_t count)
  401. {
  402. unsigned char *ptemp = NULL;
  403. size_t bytes_left = 0;
  404. size_t data_length = 0;
  405. ssize_t ret_count = 0;
  406. /* check to see if we have something to return */
  407. if ((rbu_data.image_update_buffer == NULL) ||
  408. (rbu_data.bios_image_size == 0)) {
  409. pr_debug("read_rbu_data_mono: image_update_buffer %p ,"
  410. "bios_image_size %lu\n",
  411. rbu_data.image_update_buffer,
  412. rbu_data.bios_image_size);
  413. ret_count = -ENOMEM;
  414. goto read_rbu_data_exit;
  415. }
  416. if (pos > rbu_data.bios_image_size) {
  417. ret_count = 0;
  418. goto read_rbu_data_exit;
  419. }
  420. bytes_left = rbu_data.bios_image_size - pos;
  421. data_length = min(bytes_left, count);
  422. ptemp = rbu_data.image_update_buffer;
  423. memcpy(buffer, (ptemp + pos), data_length);
  424. if ((pos + count) > rbu_data.bios_image_size)
  425. /* this was the last copy */
  426. ret_count = bytes_left;
  427. else
  428. ret_count = count;
  429. read_rbu_data_exit:
  430. return ret_count;
  431. }
  432. static ssize_t
  433. read_rbu_data(struct kobject *kobj, char *buffer, loff_t pos, size_t count)
  434. {
  435. ssize_t ret_count = 0;
  436. spin_lock(&rbu_data.lock);
  437. if (!strcmp(image_type, "mono"))
  438. ret_count = read_rbu_mono_data(buffer, pos, count);
  439. else if (!strcmp(image_type, "packet"))
  440. ret_count = read_packet_data(buffer, pos, count);
  441. else
  442. pr_debug("read_rbu_data: invalid image type specified\n");
  443. spin_unlock(&rbu_data.lock);
  444. return ret_count;
  445. }
  446. static ssize_t
  447. read_rbu_image_type(struct kobject *kobj, char *buffer, loff_t pos,
  448. size_t count)
  449. {
  450. int size = 0;
  451. if (!pos)
  452. size = sprintf(buffer, "%s\n", image_type);
  453. return size;
  454. }
  455. static ssize_t
  456. write_rbu_image_type(struct kobject *kobj, char *buffer, loff_t pos,
  457. size_t count)
  458. {
  459. int rc = count;
  460. spin_lock(&rbu_data.lock);
  461. if (strlen(buffer) < MAX_IMAGE_LENGTH)
  462. sscanf(buffer, "%s", image_type);
  463. else
  464. printk(KERN_WARNING "dell_rbu: image_type is invalid"
  465. "max chars = %d, \n incoming str--%s-- \n",
  466. MAX_IMAGE_LENGTH, buffer);
  467. /* we must free all previous allocations */
  468. packet_empty_list();
  469. img_update_free();
  470. spin_unlock(&rbu_data.lock);
  471. return rc;
  472. }
  473. static struct bin_attribute rbu_data_attr = {
  474. .attr = {.name = "data",.owner = THIS_MODULE,.mode = 0444},
  475. .read = read_rbu_data,
  476. };
  477. static struct bin_attribute rbu_image_type_attr = {
  478. .attr = {.name = "image_type",.owner = THIS_MODULE,.mode = 0644},
  479. .read = read_rbu_image_type,
  480. .write = write_rbu_image_type,
  481. };
  482. static void callbackfn_rbu(const struct firmware *fw, void *context)
  483. {
  484. int rc = 0;
  485. if (!fw || !fw->size)
  486. return;
  487. spin_lock(&rbu_data.lock);
  488. if (!strcmp(image_type, "mono")) {
  489. if (!img_update_realloc(fw->size))
  490. memcpy(rbu_data.image_update_buffer,
  491. fw->data, fw->size);
  492. } else if (!strcmp(image_type, "packet")) {
  493. if (!rbu_data.packetsize)
  494. rbu_data.packetsize = fw->size;
  495. else if (rbu_data.packetsize != fw->size) {
  496. packet_empty_list();
  497. rbu_data.packetsize = fw->size;
  498. }
  499. packetize_data(fw->data, fw->size);
  500. } else
  501. pr_debug("invalid image type specified.\n");
  502. spin_unlock(&rbu_data.lock);
  503. rc = request_firmware_nowait(THIS_MODULE, FW_ACTION_NOHOTPLUG,
  504. "dell_rbu", &rbu_device->dev,
  505. &context, callbackfn_rbu);
  506. if (rc)
  507. printk(KERN_ERR
  508. "dell_rbu:%s request_firmware_nowait failed"
  509. " %d\n", __FUNCTION__, rc);
  510. }
  511. static int __init dcdrbu_init(void)
  512. {
  513. int rc = 0;
  514. spin_lock_init(&rbu_data.lock);
  515. init_packet_head();
  516. rbu_device =
  517. platform_device_register_simple("dell_rbu", -1, NULL, 0);
  518. if (!rbu_device) {
  519. printk(KERN_ERR
  520. "dell_rbu:%s:platform_device_register_simple "
  521. "failed\n", __FUNCTION__);
  522. return -EIO;
  523. }
  524. sysfs_create_bin_file(&rbu_device->dev.kobj, &rbu_data_attr);
  525. sysfs_create_bin_file(&rbu_device->dev.kobj, &rbu_image_type_attr);
  526. rc = request_firmware_nowait(THIS_MODULE, FW_ACTION_NOHOTPLUG,
  527. "dell_rbu", &rbu_device->dev,
  528. &context, callbackfn_rbu);
  529. if (rc)
  530. printk(KERN_ERR "dell_rbu:%s:request_firmware_nowait"
  531. " failed %d\n", __FUNCTION__, rc);
  532. return rc;
  533. }
  534. static __exit void dcdrbu_exit(void)
  535. {
  536. spin_lock(&rbu_data.lock);
  537. packet_empty_list();
  538. img_update_free();
  539. spin_unlock(&rbu_data.lock);
  540. platform_device_unregister(rbu_device);
  541. }
  542. module_exit(dcdrbu_exit);
  543. module_init(dcdrbu_init);