rtas_flash.c 21 KB

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