rtas_flash.c 22 KB

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