rtas_flash.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  1. /*
  2. * c 2001 PPC 64 Team, IBM Corp
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. *
  9. * /proc/powerpc/rtas/firmware_flash interface
  10. *
  11. * This file implements a firmware_flash interface to pump a firmware
  12. * image into the kernel. At reboot time rtas_restart() will see the
  13. * firmware image and flash it as it reboots (see rtas.c).
  14. */
  15. #include <linux/module.h>
  16. #include <linux/init.h>
  17. #include <linux/slab.h>
  18. #include <linux/proc_fs.h>
  19. #include <linux/reboot.h>
  20. #include <asm/delay.h>
  21. #include <asm/uaccess.h>
  22. #include <asm/rtas.h>
  23. #define MODULE_VERS "1.0"
  24. #define MODULE_NAME "rtas_flash"
  25. #define FIRMWARE_FLASH_NAME "firmware_flash"
  26. #define FIRMWARE_UPDATE_NAME "firmware_update"
  27. #define MANAGE_FLASH_NAME "manage_flash"
  28. #define VALIDATE_FLASH_NAME "validate_flash"
  29. /* General RTAS Status Codes */
  30. #define RTAS_RC_SUCCESS 0
  31. #define RTAS_RC_HW_ERR -1
  32. #define RTAS_RC_BUSY -2
  33. /* Flash image status values */
  34. #define FLASH_AUTH -9002 /* RTAS Not Service Authority Partition */
  35. #define FLASH_NO_OP -1099 /* No operation initiated by user */
  36. #define FLASH_IMG_SHORT -1005 /* Flash image shorter than expected */
  37. #define FLASH_IMG_BAD_LEN -1004 /* Bad length value in flash list block */
  38. #define FLASH_IMG_NULL_DATA -1003 /* Bad data value in flash list block */
  39. #define FLASH_IMG_READY 0 /* Firmware img ready for flash on reboot */
  40. /* Manage image status values */
  41. #define MANAGE_AUTH -9002 /* RTAS Not Service Authority Partition */
  42. #define MANAGE_ACTIVE_ERR -9001 /* RTAS Cannot Overwrite Active Img */
  43. #define MANAGE_NO_OP -1099 /* No operation initiated by user */
  44. #define MANAGE_PARAM_ERR -3 /* RTAS Parameter Error */
  45. #define MANAGE_HW_ERR -1 /* RTAS Hardware Error */
  46. /* Validate image status values */
  47. #define VALIDATE_AUTH -9002 /* RTAS Not Service Authority Partition */
  48. #define VALIDATE_NO_OP -1099 /* No operation initiated by the user */
  49. #define VALIDATE_INCOMPLETE -1002 /* User copied < VALIDATE_BUF_SIZE */
  50. #define VALIDATE_READY -1001 /* Firmware image ready for validation */
  51. #define VALIDATE_PARAM_ERR -3 /* RTAS Parameter Error */
  52. #define VALIDATE_HW_ERR -1 /* RTAS Hardware Error */
  53. /* ibm,validate-flash-image update result tokens */
  54. #define VALIDATE_TMP_UPDATE 0 /* T side will be updated */
  55. #define VALIDATE_FLASH_AUTH 1 /* Partition does not have authority */
  56. #define VALIDATE_INVALID_IMG 2 /* Candidate image is not valid */
  57. #define VALIDATE_CUR_UNKNOWN 3 /* Current fixpack level is unknown */
  58. /*
  59. * Current T side will be committed to P side before being replace with new
  60. * image, and the new image is downlevel from current image
  61. */
  62. #define VALIDATE_TMP_COMMIT_DL 4
  63. /*
  64. * Current T side will be committed to P side before being replaced with new
  65. * image
  66. */
  67. #define VALIDATE_TMP_COMMIT 5
  68. /*
  69. * T side will be updated with a downlevel image
  70. */
  71. #define VALIDATE_TMP_UPDATE_DL 6
  72. /*
  73. * The candidate image's release date is later than the system's firmware
  74. * service entitlement date - service warranty period has expired
  75. */
  76. #define VALIDATE_OUT_OF_WRNTY 7
  77. /* ibm,manage-flash-image operation tokens */
  78. #define RTAS_REJECT_TMP_IMG 0
  79. #define RTAS_COMMIT_TMP_IMG 1
  80. /* Array sizes */
  81. #define VALIDATE_BUF_SIZE 4096
  82. #define RTAS_MSG_MAXLEN 64
  83. /* Quirk - RTAS requires 4k list length and block size */
  84. #define RTAS_BLKLIST_LENGTH 4096
  85. #define RTAS_BLK_SIZE 4096
  86. struct flash_block {
  87. char *data;
  88. unsigned long length;
  89. };
  90. /* This struct is very similar but not identical to
  91. * that needed by the rtas flash update.
  92. * All we need to do for rtas is rewrite num_blocks
  93. * into a version/length and translate the pointers
  94. * to absolute.
  95. */
  96. #define FLASH_BLOCKS_PER_NODE ((RTAS_BLKLIST_LENGTH - 16) / sizeof(struct flash_block))
  97. struct flash_block_list {
  98. unsigned long num_blocks;
  99. struct flash_block_list *next;
  100. struct flash_block blocks[FLASH_BLOCKS_PER_NODE];
  101. };
  102. static struct flash_block_list *rtas_firmware_flash_list;
  103. /* Use slab cache to guarantee 4k alignment */
  104. static struct kmem_cache *flash_block_cache = NULL;
  105. #define FLASH_BLOCK_LIST_VERSION (1UL)
  106. /*
  107. * Local copy of the flash block list.
  108. *
  109. * The rtas_firmware_flash_list varable will be
  110. * set once the data is fully read.
  111. *
  112. * For convenience as we build the list we use virtual addrs,
  113. * we do not fill in the version number, and the length field
  114. * is treated as the number of entries currently in the block
  115. * (i.e. not a byte count). This is all fixed when calling
  116. * the flash routine.
  117. */
  118. /* Status int must be first member of struct */
  119. struct rtas_update_flash_t
  120. {
  121. int status; /* Flash update status */
  122. struct flash_block_list *flist; /* Local copy of flash block list */
  123. };
  124. /* Status int must be first member of struct */
  125. struct rtas_manage_flash_t
  126. {
  127. int status; /* Returned status */
  128. };
  129. /* Status int must be first member of struct */
  130. struct rtas_validate_flash_t
  131. {
  132. int status; /* Returned status */
  133. char *buf; /* Candidate image buffer */
  134. unsigned int buf_size; /* Size of image buf */
  135. unsigned int update_results; /* Update results token */
  136. };
  137. static struct rtas_update_flash_t rtas_update_flash_data;
  138. static struct rtas_manage_flash_t rtas_manage_flash_data;
  139. static struct rtas_validate_flash_t rtas_validate_flash_data;
  140. static DEFINE_MUTEX(rtas_update_flash_mutex);
  141. static DEFINE_MUTEX(rtas_manage_flash_mutex);
  142. static DEFINE_MUTEX(rtas_validate_flash_mutex);
  143. /* Do simple sanity checks on the flash image. */
  144. static int flash_list_valid(struct flash_block_list *flist)
  145. {
  146. struct flash_block_list *f;
  147. int i;
  148. unsigned long block_size, image_size;
  149. /* Paranoid self test here. We also collect the image size. */
  150. image_size = 0;
  151. for (f = flist; f; f = f->next) {
  152. for (i = 0; i < f->num_blocks; i++) {
  153. if (f->blocks[i].data == NULL) {
  154. return FLASH_IMG_NULL_DATA;
  155. }
  156. block_size = f->blocks[i].length;
  157. if (block_size <= 0 || block_size > RTAS_BLK_SIZE) {
  158. return FLASH_IMG_BAD_LEN;
  159. }
  160. image_size += block_size;
  161. }
  162. }
  163. if (image_size < (256 << 10)) {
  164. if (image_size < 2)
  165. return FLASH_NO_OP;
  166. }
  167. printk(KERN_INFO "FLASH: flash image with %ld bytes stored for hardware flash on reboot\n", image_size);
  168. return FLASH_IMG_READY;
  169. }
  170. static void free_flash_list(struct flash_block_list *f)
  171. {
  172. struct flash_block_list *next;
  173. int i;
  174. while (f) {
  175. for (i = 0; i < f->num_blocks; i++)
  176. kmem_cache_free(flash_block_cache, f->blocks[i].data);
  177. next = f->next;
  178. kmem_cache_free(flash_block_cache, f);
  179. f = next;
  180. }
  181. }
  182. static int rtas_flash_release(struct inode *inode, struct file *file)
  183. {
  184. struct rtas_update_flash_t *const uf = &rtas_update_flash_data;
  185. mutex_lock(&rtas_update_flash_mutex);
  186. if (uf->flist) {
  187. /* File was opened in write mode for a new flash attempt */
  188. /* Clear saved list */
  189. if (rtas_firmware_flash_list) {
  190. free_flash_list(rtas_firmware_flash_list);
  191. rtas_firmware_flash_list = NULL;
  192. }
  193. if (uf->status != FLASH_AUTH)
  194. uf->status = flash_list_valid(uf->flist);
  195. if (uf->status == FLASH_IMG_READY)
  196. rtas_firmware_flash_list = uf->flist;
  197. else
  198. free_flash_list(uf->flist);
  199. uf->flist = NULL;
  200. }
  201. mutex_unlock(&rtas_update_flash_mutex);
  202. return 0;
  203. }
  204. static size_t get_flash_status_msg(int status, char *buf)
  205. {
  206. const char *msg;
  207. size_t len;
  208. switch (status) {
  209. case FLASH_AUTH:
  210. msg = "error: this partition does not have service authority\n";
  211. break;
  212. case FLASH_NO_OP:
  213. msg = "info: no firmware image for flash\n";
  214. break;
  215. case FLASH_IMG_SHORT:
  216. msg = "error: flash image short\n";
  217. break;
  218. case FLASH_IMG_BAD_LEN:
  219. msg = "error: internal error bad length\n";
  220. break;
  221. case FLASH_IMG_NULL_DATA:
  222. msg = "error: internal error null data\n";
  223. break;
  224. case FLASH_IMG_READY:
  225. msg = "ready: firmware image ready for flash on reboot\n";
  226. break;
  227. default:
  228. return sprintf(buf, "error: unexpected status value %d\n",
  229. status);
  230. }
  231. len = strlen(msg);
  232. memcpy(buf, msg, len + 1);
  233. return len;
  234. }
  235. /* Reading the proc file will show status (not the firmware contents) */
  236. static ssize_t rtas_flash_read_msg(struct file *file, char __user *buf,
  237. size_t count, loff_t *ppos)
  238. {
  239. struct rtas_update_flash_t *const uf = &rtas_update_flash_data;
  240. char msg[RTAS_MSG_MAXLEN];
  241. size_t len;
  242. int status;
  243. mutex_lock(&rtas_update_flash_mutex);
  244. status = uf->status;
  245. mutex_unlock(&rtas_update_flash_mutex);
  246. /* Read as text message */
  247. len = get_flash_status_msg(status, msg);
  248. return simple_read_from_buffer(buf, count, ppos, msg, len);
  249. }
  250. static ssize_t rtas_flash_read_num(struct file *file, char __user *buf,
  251. size_t count, loff_t *ppos)
  252. {
  253. struct rtas_update_flash_t *const uf = &rtas_update_flash_data;
  254. char msg[RTAS_MSG_MAXLEN];
  255. int status;
  256. mutex_lock(&rtas_update_flash_mutex);
  257. status = uf->status;
  258. mutex_unlock(&rtas_update_flash_mutex);
  259. /* Read as number */
  260. sprintf(msg, "%d\n", status);
  261. return simple_read_from_buffer(buf, count, ppos, msg, strlen(msg));
  262. }
  263. /* We could be much more efficient here. But to keep this function
  264. * simple we allocate a page to the block list no matter how small the
  265. * count is. If the system is low on memory it will be just as well
  266. * that we fail....
  267. */
  268. static ssize_t rtas_flash_write(struct file *file, const char __user *buffer,
  269. size_t count, loff_t *off)
  270. {
  271. struct rtas_update_flash_t *const uf = &rtas_update_flash_data;
  272. char *p;
  273. int next_free, rc;
  274. struct flash_block_list *fl;
  275. mutex_lock(&rtas_update_flash_mutex);
  276. if (uf->status == FLASH_AUTH || count == 0)
  277. goto out; /* discard data */
  278. /* In the case that the image is not ready for flashing, the memory
  279. * allocated for the block list will be freed upon the release of the
  280. * proc file
  281. */
  282. if (uf->flist == NULL) {
  283. uf->flist = kmem_cache_zalloc(flash_block_cache, GFP_KERNEL);
  284. if (!uf->flist)
  285. goto nomem;
  286. }
  287. fl = uf->flist;
  288. while (fl->next)
  289. fl = fl->next; /* seek to last block_list for append */
  290. next_free = fl->num_blocks;
  291. if (next_free == FLASH_BLOCKS_PER_NODE) {
  292. /* Need to allocate another block_list */
  293. fl->next = kmem_cache_zalloc(flash_block_cache, GFP_KERNEL);
  294. if (!fl->next)
  295. goto nomem;
  296. fl = fl->next;
  297. next_free = 0;
  298. }
  299. if (count > RTAS_BLK_SIZE)
  300. count = RTAS_BLK_SIZE;
  301. p = kmem_cache_zalloc(flash_block_cache, GFP_KERNEL);
  302. if (!p)
  303. goto nomem;
  304. if(copy_from_user(p, buffer, count)) {
  305. kmem_cache_free(flash_block_cache, p);
  306. rc = -EFAULT;
  307. goto error;
  308. }
  309. fl->blocks[next_free].data = p;
  310. fl->blocks[next_free].length = count;
  311. fl->num_blocks++;
  312. out:
  313. mutex_unlock(&rtas_update_flash_mutex);
  314. return count;
  315. nomem:
  316. rc = -ENOMEM;
  317. error:
  318. mutex_unlock(&rtas_update_flash_mutex);
  319. return rc;
  320. }
  321. /*
  322. * Flash management routines.
  323. */
  324. static void manage_flash(struct rtas_manage_flash_t *args_buf, unsigned int op)
  325. {
  326. s32 rc;
  327. do {
  328. rc = rtas_call(rtas_token("ibm,manage-flash-image"), 1, 1,
  329. NULL, op);
  330. } while (rtas_busy_delay(rc));
  331. args_buf->status = rc;
  332. }
  333. static ssize_t manage_flash_read(struct file *file, char __user *buf,
  334. size_t count, loff_t *ppos)
  335. {
  336. struct rtas_manage_flash_t *const args_buf = &rtas_manage_flash_data;
  337. char msg[RTAS_MSG_MAXLEN];
  338. int msglen, status;
  339. mutex_lock(&rtas_manage_flash_mutex);
  340. status = args_buf->status;
  341. mutex_unlock(&rtas_manage_flash_mutex);
  342. msglen = sprintf(msg, "%d\n", status);
  343. return simple_read_from_buffer(buf, count, ppos, msg, msglen);
  344. }
  345. static ssize_t manage_flash_write(struct file *file, const char __user *buf,
  346. size_t count, loff_t *off)
  347. {
  348. struct rtas_manage_flash_t *const args_buf = &rtas_manage_flash_data;
  349. static const char reject_str[] = "0";
  350. static const char commit_str[] = "1";
  351. char stkbuf[10];
  352. int op, rc;
  353. mutex_lock(&rtas_manage_flash_mutex);
  354. if ((args_buf->status == MANAGE_AUTH) || (count == 0))
  355. goto out;
  356. op = -1;
  357. if (buf) {
  358. if (count > 9) count = 9;
  359. rc = -EFAULT;
  360. if (copy_from_user (stkbuf, buf, count))
  361. goto error;
  362. if (strncmp(stkbuf, reject_str, strlen(reject_str)) == 0)
  363. op = RTAS_REJECT_TMP_IMG;
  364. else if (strncmp(stkbuf, commit_str, strlen(commit_str)) == 0)
  365. op = RTAS_COMMIT_TMP_IMG;
  366. }
  367. if (op == -1) { /* buf is empty, or contains invalid string */
  368. rc = -EINVAL;
  369. goto error;
  370. }
  371. manage_flash(args_buf, op);
  372. out:
  373. mutex_unlock(&rtas_manage_flash_mutex);
  374. return count;
  375. error:
  376. mutex_unlock(&rtas_manage_flash_mutex);
  377. return rc;
  378. }
  379. /*
  380. * Validation routines.
  381. */
  382. static void validate_flash(struct rtas_validate_flash_t *args_buf)
  383. {
  384. int token = rtas_token("ibm,validate-flash-image");
  385. int update_results;
  386. s32 rc;
  387. rc = 0;
  388. do {
  389. spin_lock(&rtas_data_buf_lock);
  390. memcpy(rtas_data_buf, args_buf->buf, VALIDATE_BUF_SIZE);
  391. rc = rtas_call(token, 2, 2, &update_results,
  392. (u32) __pa(rtas_data_buf), args_buf->buf_size);
  393. memcpy(args_buf->buf, rtas_data_buf, VALIDATE_BUF_SIZE);
  394. spin_unlock(&rtas_data_buf_lock);
  395. } while (rtas_busy_delay(rc));
  396. args_buf->status = rc;
  397. args_buf->update_results = update_results;
  398. }
  399. static int get_validate_flash_msg(struct rtas_validate_flash_t *args_buf,
  400. char *msg)
  401. {
  402. int n;
  403. if (args_buf->status >= VALIDATE_TMP_UPDATE) {
  404. n = sprintf(msg, "%d\n", args_buf->update_results);
  405. if ((args_buf->update_results >= VALIDATE_CUR_UNKNOWN) ||
  406. (args_buf->update_results == VALIDATE_TMP_UPDATE))
  407. n += sprintf(msg + n, "%s\n", args_buf->buf);
  408. } else {
  409. n = sprintf(msg, "%d\n", args_buf->status);
  410. }
  411. return n;
  412. }
  413. static ssize_t validate_flash_read(struct file *file, char __user *buf,
  414. size_t count, loff_t *ppos)
  415. {
  416. struct rtas_validate_flash_t *const args_buf =
  417. &rtas_validate_flash_data;
  418. char msg[RTAS_MSG_MAXLEN];
  419. int msglen;
  420. mutex_lock(&rtas_validate_flash_mutex);
  421. msglen = get_validate_flash_msg(args_buf, msg);
  422. mutex_unlock(&rtas_validate_flash_mutex);
  423. return simple_read_from_buffer(buf, count, ppos, msg, msglen);
  424. }
  425. static ssize_t validate_flash_write(struct file *file, const char __user *buf,
  426. size_t count, loff_t *off)
  427. {
  428. struct rtas_validate_flash_t *const args_buf =
  429. &rtas_validate_flash_data;
  430. int rc;
  431. mutex_lock(&rtas_validate_flash_mutex);
  432. /* We are only interested in the first 4K of the
  433. * candidate image */
  434. if ((*off >= VALIDATE_BUF_SIZE) ||
  435. (args_buf->status == VALIDATE_AUTH)) {
  436. *off += count;
  437. mutex_unlock(&rtas_validate_flash_mutex);
  438. return count;
  439. }
  440. if (*off + count >= VALIDATE_BUF_SIZE) {
  441. count = VALIDATE_BUF_SIZE - *off;
  442. args_buf->status = VALIDATE_READY;
  443. } else {
  444. args_buf->status = VALIDATE_INCOMPLETE;
  445. }
  446. if (!access_ok(VERIFY_READ, buf, count)) {
  447. rc = -EFAULT;
  448. goto done;
  449. }
  450. if (copy_from_user(args_buf->buf + *off, buf, count)) {
  451. rc = -EFAULT;
  452. goto done;
  453. }
  454. *off += count;
  455. rc = count;
  456. done:
  457. mutex_unlock(&rtas_validate_flash_mutex);
  458. return rc;
  459. }
  460. static int validate_flash_release(struct inode *inode, struct file *file)
  461. {
  462. struct rtas_validate_flash_t *const args_buf =
  463. &rtas_validate_flash_data;
  464. mutex_lock(&rtas_validate_flash_mutex);
  465. if (args_buf->status == VALIDATE_READY) {
  466. args_buf->buf_size = VALIDATE_BUF_SIZE;
  467. validate_flash(args_buf);
  468. }
  469. mutex_unlock(&rtas_validate_flash_mutex);
  470. return 0;
  471. }
  472. /*
  473. * On-reboot flash update applicator.
  474. */
  475. static void rtas_flash_firmware(int reboot_type)
  476. {
  477. unsigned long image_size;
  478. struct flash_block_list *f, *next, *flist;
  479. unsigned long rtas_block_list;
  480. int i, status, update_token;
  481. if (rtas_firmware_flash_list == NULL)
  482. return; /* nothing to do */
  483. if (reboot_type != SYS_RESTART) {
  484. printk(KERN_ALERT "FLASH: firmware flash requires a reboot\n");
  485. printk(KERN_ALERT "FLASH: the firmware image will NOT be flashed\n");
  486. return;
  487. }
  488. update_token = rtas_token("ibm,update-flash-64-and-reboot");
  489. if (update_token == RTAS_UNKNOWN_SERVICE) {
  490. printk(KERN_ALERT "FLASH: ibm,update-flash-64-and-reboot "
  491. "is not available -- not a service partition?\n");
  492. printk(KERN_ALERT "FLASH: firmware will not be flashed\n");
  493. return;
  494. }
  495. /*
  496. * Just before starting the firmware flash, cancel the event scan work
  497. * to avoid any soft lockup issues.
  498. */
  499. rtas_cancel_event_scan();
  500. /*
  501. * NOTE: the "first" block must be under 4GB, so we create
  502. * an entry with no data blocks in the reserved buffer in
  503. * the kernel data segment.
  504. */
  505. spin_lock(&rtas_data_buf_lock);
  506. flist = (struct flash_block_list *)&rtas_data_buf[0];
  507. flist->num_blocks = 0;
  508. flist->next = rtas_firmware_flash_list;
  509. rtas_block_list = __pa(flist);
  510. if (rtas_block_list >= 4UL*1024*1024*1024) {
  511. printk(KERN_ALERT "FLASH: kernel bug...flash list header addr above 4GB\n");
  512. spin_unlock(&rtas_data_buf_lock);
  513. return;
  514. }
  515. printk(KERN_ALERT "FLASH: preparing saved firmware image for flash\n");
  516. /* Update the block_list in place. */
  517. rtas_firmware_flash_list = NULL; /* too hard to backout on error */
  518. image_size = 0;
  519. for (f = flist; f; f = next) {
  520. /* Translate data addrs to absolute */
  521. for (i = 0; i < f->num_blocks; i++) {
  522. f->blocks[i].data = (char *)__pa(f->blocks[i].data);
  523. image_size += f->blocks[i].length;
  524. }
  525. next = f->next;
  526. /* Don't translate NULL pointer for last entry */
  527. if (f->next)
  528. f->next = (struct flash_block_list *)__pa(f->next);
  529. else
  530. f->next = NULL;
  531. /* make num_blocks into the version/length field */
  532. f->num_blocks = (FLASH_BLOCK_LIST_VERSION << 56) | ((f->num_blocks+1)*16);
  533. }
  534. printk(KERN_ALERT "FLASH: flash image is %ld bytes\n", image_size);
  535. printk(KERN_ALERT "FLASH: performing flash and reboot\n");
  536. rtas_progress("Flashing \n", 0x0);
  537. rtas_progress("Please Wait... ", 0x0);
  538. printk(KERN_ALERT "FLASH: this will take several minutes. Do not power off!\n");
  539. status = rtas_call(update_token, 1, 1, NULL, rtas_block_list);
  540. switch (status) { /* should only get "bad" status */
  541. case 0:
  542. printk(KERN_ALERT "FLASH: success\n");
  543. break;
  544. case -1:
  545. printk(KERN_ALERT "FLASH: hardware error. Firmware may not be not flashed\n");
  546. break;
  547. case -3:
  548. printk(KERN_ALERT "FLASH: image is corrupt or not correct for this platform. Firmware not flashed\n");
  549. break;
  550. case -4:
  551. printk(KERN_ALERT "FLASH: flash failed when partially complete. System may not reboot\n");
  552. break;
  553. default:
  554. printk(KERN_ALERT "FLASH: unknown flash return code %d\n", status);
  555. break;
  556. }
  557. spin_unlock(&rtas_data_buf_lock);
  558. }
  559. /*
  560. * Manifest of proc files to create
  561. */
  562. struct rtas_flash_file {
  563. const char *filename;
  564. const char *rtas_call_name;
  565. int *status;
  566. const struct file_operations fops;
  567. };
  568. static const struct rtas_flash_file rtas_flash_files[] = {
  569. {
  570. .filename = "powerpc/rtas/" FIRMWARE_FLASH_NAME,
  571. .rtas_call_name = "ibm,update-flash-64-and-reboot",
  572. .status = &rtas_update_flash_data.status,
  573. .fops.read = rtas_flash_read_msg,
  574. .fops.write = rtas_flash_write,
  575. .fops.release = rtas_flash_release,
  576. .fops.llseek = default_llseek,
  577. },
  578. {
  579. .filename = "powerpc/rtas/" FIRMWARE_UPDATE_NAME,
  580. .rtas_call_name = "ibm,update-flash-64-and-reboot",
  581. .status = &rtas_update_flash_data.status,
  582. .fops.read = rtas_flash_read_num,
  583. .fops.write = rtas_flash_write,
  584. .fops.release = rtas_flash_release,
  585. .fops.llseek = default_llseek,
  586. },
  587. {
  588. .filename = "powerpc/rtas/" VALIDATE_FLASH_NAME,
  589. .rtas_call_name = "ibm,validate-flash-image",
  590. .status = &rtas_validate_flash_data.status,
  591. .fops.read = validate_flash_read,
  592. .fops.write = validate_flash_write,
  593. .fops.release = validate_flash_release,
  594. .fops.llseek = default_llseek,
  595. },
  596. {
  597. .filename = "powerpc/rtas/" MANAGE_FLASH_NAME,
  598. .rtas_call_name = "ibm,manage-flash-image",
  599. .status = &rtas_manage_flash_data.status,
  600. .fops.read = manage_flash_read,
  601. .fops.write = manage_flash_write,
  602. .fops.llseek = default_llseek,
  603. }
  604. };
  605. static int __init rtas_flash_init(void)
  606. {
  607. int i;
  608. if (rtas_token("ibm,update-flash-64-and-reboot") ==
  609. RTAS_UNKNOWN_SERVICE) {
  610. pr_info("rtas_flash: no firmware flash support\n");
  611. return 1;
  612. }
  613. rtas_validate_flash_data.buf = kzalloc(VALIDATE_BUF_SIZE, GFP_KERNEL);
  614. if (!rtas_validate_flash_data.buf)
  615. return -ENOMEM;
  616. flash_block_cache = kmem_cache_create("rtas_flash_cache",
  617. RTAS_BLK_SIZE, RTAS_BLK_SIZE, 0,
  618. NULL);
  619. if (!flash_block_cache) {
  620. printk(KERN_ERR "%s: failed to create block cache\n",
  621. __func__);
  622. goto enomem_buf;
  623. }
  624. for (i = 0; i < ARRAY_SIZE(rtas_flash_files); i++) {
  625. const struct rtas_flash_file *f = &rtas_flash_files[i];
  626. int token;
  627. if (!proc_create(f->filename, S_IRUSR | S_IWUSR, NULL, &f->fops))
  628. goto enomem;
  629. /*
  630. * This code assumes that the status int is the first member of the
  631. * struct
  632. */
  633. token = rtas_token(f->rtas_call_name);
  634. if (token == RTAS_UNKNOWN_SERVICE)
  635. *f->status = FLASH_AUTH;
  636. else
  637. *f->status = FLASH_NO_OP;
  638. }
  639. rtas_flash_term_hook = rtas_flash_firmware;
  640. return 0;
  641. enomem:
  642. while (--i >= 0) {
  643. const struct rtas_flash_file *f = &rtas_flash_files[i];
  644. remove_proc_entry(f->filename, NULL);
  645. }
  646. kmem_cache_destroy(flash_block_cache);
  647. enomem_buf:
  648. kfree(rtas_validate_flash_data.buf);
  649. return -ENOMEM;
  650. }
  651. static void __exit rtas_flash_cleanup(void)
  652. {
  653. int i;
  654. rtas_flash_term_hook = NULL;
  655. if (rtas_firmware_flash_list) {
  656. free_flash_list(rtas_firmware_flash_list);
  657. rtas_firmware_flash_list = NULL;
  658. }
  659. for (i = 0; i < ARRAY_SIZE(rtas_flash_files); i++) {
  660. const struct rtas_flash_file *f = &rtas_flash_files[i];
  661. remove_proc_entry(f->filename, NULL);
  662. }
  663. kmem_cache_destroy(flash_block_cache);
  664. kfree(rtas_validate_flash_data.buf);
  665. }
  666. module_init(rtas_flash_init);
  667. module_exit(rtas_flash_cleanup);
  668. MODULE_LICENSE("GPL");