rtas_flash.c 21 KB

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