rtas_flash.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  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. /* constructor for flash_block_cache */
  248. void rtas_block_ctor(void *ptr)
  249. {
  250. memset(ptr, 0, RTAS_BLK_SIZE);
  251. }
  252. /* We could be much more efficient here. But to keep this function
  253. * simple we allocate a page to the block list no matter how small the
  254. * count is. If the system is low on memory it will be just as well
  255. * that we fail....
  256. */
  257. static ssize_t rtas_flash_write(struct file *file, const char __user *buffer,
  258. size_t count, loff_t *off)
  259. {
  260. struct proc_dir_entry *dp = PDE(file_inode(file));
  261. struct rtas_update_flash_t *uf;
  262. char *p;
  263. int next_free;
  264. struct flash_block_list *fl;
  265. uf = (struct rtas_update_flash_t *) dp->data;
  266. if (uf->status == FLASH_AUTH || count == 0)
  267. return count; /* discard data */
  268. /* In the case that the image is not ready for flashing, the memory
  269. * allocated for the block list will be freed upon the release of the
  270. * proc file
  271. */
  272. if (uf->flist == NULL) {
  273. uf->flist = kmem_cache_alloc(flash_block_cache, GFP_KERNEL);
  274. if (!uf->flist)
  275. return -ENOMEM;
  276. }
  277. fl = uf->flist;
  278. while (fl->next)
  279. fl = fl->next; /* seek to last block_list for append */
  280. next_free = fl->num_blocks;
  281. if (next_free == FLASH_BLOCKS_PER_NODE) {
  282. /* Need to allocate another block_list */
  283. fl->next = kmem_cache_alloc(flash_block_cache, GFP_KERNEL);
  284. if (!fl->next)
  285. return -ENOMEM;
  286. fl = fl->next;
  287. next_free = 0;
  288. }
  289. if (count > RTAS_BLK_SIZE)
  290. count = RTAS_BLK_SIZE;
  291. p = kmem_cache_alloc(flash_block_cache, GFP_KERNEL);
  292. if (!p)
  293. return -ENOMEM;
  294. if(copy_from_user(p, buffer, count)) {
  295. kmem_cache_free(flash_block_cache, p);
  296. return -EFAULT;
  297. }
  298. fl->blocks[next_free].data = p;
  299. fl->blocks[next_free].length = count;
  300. fl->num_blocks++;
  301. return count;
  302. }
  303. static int rtas_excl_open(struct inode *inode, struct file *file)
  304. {
  305. struct proc_dir_entry *dp = PDE(inode);
  306. /* Enforce exclusive open with use count of PDE */
  307. spin_lock(&flash_file_open_lock);
  308. if (atomic_read(&dp->count) > 2) {
  309. spin_unlock(&flash_file_open_lock);
  310. return -EBUSY;
  311. }
  312. atomic_inc(&dp->count);
  313. spin_unlock(&flash_file_open_lock);
  314. return 0;
  315. }
  316. static int rtas_excl_release(struct inode *inode, struct file *file)
  317. {
  318. struct proc_dir_entry *dp = PDE(inode);
  319. atomic_dec(&dp->count);
  320. return 0;
  321. }
  322. static void manage_flash(struct rtas_manage_flash_t *args_buf)
  323. {
  324. s32 rc;
  325. do {
  326. rc = rtas_call(rtas_token("ibm,manage-flash-image"), 1,
  327. 1, NULL, args_buf->op);
  328. } while (rtas_busy_delay(rc));
  329. args_buf->status = rc;
  330. }
  331. static ssize_t manage_flash_read(struct file *file, char __user *buf,
  332. size_t count, loff_t *ppos)
  333. {
  334. struct proc_dir_entry *dp = PDE(file_inode(file));
  335. struct rtas_manage_flash_t *args_buf;
  336. char msg[RTAS_MSG_MAXLEN];
  337. int msglen;
  338. args_buf = dp->data;
  339. if (args_buf == NULL)
  340. return 0;
  341. msglen = sprintf(msg, "%d\n", args_buf->status);
  342. return simple_read_from_buffer(buf, count, ppos, msg, msglen);
  343. }
  344. static ssize_t manage_flash_write(struct file *file, const char __user *buf,
  345. size_t count, loff_t *off)
  346. {
  347. struct proc_dir_entry *dp = PDE(file_inode(file));
  348. struct rtas_manage_flash_t *args_buf;
  349. const char reject_str[] = "0";
  350. const char commit_str[] = "1";
  351. char stkbuf[10];
  352. int op;
  353. args_buf = (struct rtas_manage_flash_t *) dp->data;
  354. if ((args_buf->status == MANAGE_AUTH) || (count == 0))
  355. return count;
  356. op = -1;
  357. if (buf) {
  358. if (count > 9) count = 9;
  359. if (copy_from_user (stkbuf, buf, count)) {
  360. return -EFAULT;
  361. }
  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. return -EINVAL;
  369. args_buf->op = op;
  370. manage_flash(args_buf);
  371. return count;
  372. }
  373. static void validate_flash(struct rtas_validate_flash_t *args_buf)
  374. {
  375. int token = rtas_token("ibm,validate-flash-image");
  376. int update_results;
  377. s32 rc;
  378. rc = 0;
  379. do {
  380. spin_lock(&rtas_data_buf_lock);
  381. memcpy(rtas_data_buf, args_buf->buf, VALIDATE_BUF_SIZE);
  382. rc = rtas_call(token, 2, 2, &update_results,
  383. (u32) __pa(rtas_data_buf), args_buf->buf_size);
  384. memcpy(args_buf->buf, rtas_data_buf, VALIDATE_BUF_SIZE);
  385. spin_unlock(&rtas_data_buf_lock);
  386. } while (rtas_busy_delay(rc));
  387. args_buf->status = rc;
  388. args_buf->update_results = update_results;
  389. }
  390. static int get_validate_flash_msg(struct rtas_validate_flash_t *args_buf,
  391. char *msg)
  392. {
  393. int n;
  394. if (args_buf->status >= VALIDATE_TMP_UPDATE) {
  395. n = sprintf(msg, "%d\n", args_buf->update_results);
  396. if ((args_buf->update_results >= VALIDATE_CUR_UNKNOWN) ||
  397. (args_buf->update_results == VALIDATE_TMP_UPDATE))
  398. n += sprintf(msg + n, "%s\n", args_buf->buf);
  399. } else {
  400. n = sprintf(msg, "%d\n", args_buf->status);
  401. }
  402. return n;
  403. }
  404. static ssize_t validate_flash_read(struct file *file, char __user *buf,
  405. size_t count, loff_t *ppos)
  406. {
  407. struct proc_dir_entry *dp = PDE(file_inode(file));
  408. struct rtas_validate_flash_t *args_buf;
  409. char msg[RTAS_MSG_MAXLEN];
  410. int msglen;
  411. args_buf = dp->data;
  412. msglen = get_validate_flash_msg(args_buf, msg);
  413. return simple_read_from_buffer(buf, count, ppos, msg, msglen);
  414. }
  415. static ssize_t validate_flash_write(struct file *file, const char __user *buf,
  416. size_t count, loff_t *off)
  417. {
  418. struct proc_dir_entry *dp = PDE(file_inode(file));
  419. struct rtas_validate_flash_t *args_buf;
  420. int rc;
  421. args_buf = (struct rtas_validate_flash_t *) dp->data;
  422. if (dp->data == NULL) {
  423. dp->data = kmalloc(sizeof(struct rtas_validate_flash_t),
  424. GFP_KERNEL);
  425. if (dp->data == NULL)
  426. return -ENOMEM;
  427. }
  428. /* We are only interested in the first 4K of the
  429. * candidate image */
  430. if ((*off >= VALIDATE_BUF_SIZE) ||
  431. (args_buf->status == VALIDATE_AUTH)) {
  432. *off += count;
  433. return count;
  434. }
  435. if (*off + count >= VALIDATE_BUF_SIZE) {
  436. count = VALIDATE_BUF_SIZE - *off;
  437. args_buf->status = VALIDATE_READY;
  438. } else {
  439. args_buf->status = VALIDATE_INCOMPLETE;
  440. }
  441. if (!access_ok(VERIFY_READ, buf, count)) {
  442. rc = -EFAULT;
  443. goto done;
  444. }
  445. if (copy_from_user(args_buf->buf + *off, buf, count)) {
  446. rc = -EFAULT;
  447. goto done;
  448. }
  449. *off += count;
  450. rc = count;
  451. done:
  452. if (rc < 0) {
  453. kfree(dp->data);
  454. dp->data = NULL;
  455. }
  456. return rc;
  457. }
  458. static int validate_flash_release(struct inode *inode, struct file *file)
  459. {
  460. struct proc_dir_entry *dp = PDE(file_inode(file));
  461. struct rtas_validate_flash_t *args_buf;
  462. args_buf = (struct rtas_validate_flash_t *) dp->data;
  463. if (args_buf->status == VALIDATE_READY) {
  464. args_buf->buf_size = VALIDATE_BUF_SIZE;
  465. validate_flash(args_buf);
  466. }
  467. /* The matching atomic_inc was in rtas_excl_open() */
  468. atomic_dec(&dp->count);
  469. return 0;
  470. }
  471. static void rtas_flash_firmware(int reboot_type)
  472. {
  473. unsigned long image_size;
  474. struct flash_block_list *f, *next, *flist;
  475. unsigned long rtas_block_list;
  476. int i, status, update_token;
  477. if (rtas_firmware_flash_list == NULL)
  478. return; /* nothing to do */
  479. if (reboot_type != SYS_RESTART) {
  480. printk(KERN_ALERT "FLASH: firmware flash requires a reboot\n");
  481. printk(KERN_ALERT "FLASH: the firmware image will NOT be flashed\n");
  482. return;
  483. }
  484. update_token = rtas_token("ibm,update-flash-64-and-reboot");
  485. if (update_token == RTAS_UNKNOWN_SERVICE) {
  486. printk(KERN_ALERT "FLASH: ibm,update-flash-64-and-reboot "
  487. "is not available -- not a service partition?\n");
  488. printk(KERN_ALERT "FLASH: firmware will not be flashed\n");
  489. return;
  490. }
  491. /*
  492. * Just before starting the firmware flash, cancel the event scan work
  493. * to avoid any soft lockup issues.
  494. */
  495. rtas_cancel_event_scan();
  496. /*
  497. * NOTE: the "first" block must be under 4GB, so we create
  498. * an entry with no data blocks in the reserved buffer in
  499. * the kernel data segment.
  500. */
  501. spin_lock(&rtas_data_buf_lock);
  502. flist = (struct flash_block_list *)&rtas_data_buf[0];
  503. flist->num_blocks = 0;
  504. flist->next = rtas_firmware_flash_list;
  505. rtas_block_list = __pa(flist);
  506. if (rtas_block_list >= 4UL*1024*1024*1024) {
  507. printk(KERN_ALERT "FLASH: kernel bug...flash list header addr above 4GB\n");
  508. spin_unlock(&rtas_data_buf_lock);
  509. return;
  510. }
  511. printk(KERN_ALERT "FLASH: preparing saved firmware image for flash\n");
  512. /* Update the block_list in place. */
  513. rtas_firmware_flash_list = NULL; /* too hard to backout on error */
  514. image_size = 0;
  515. for (f = flist; f; f = next) {
  516. /* Translate data addrs to absolute */
  517. for (i = 0; i < f->num_blocks; i++) {
  518. f->blocks[i].data = (char *)__pa(f->blocks[i].data);
  519. image_size += f->blocks[i].length;
  520. }
  521. next = f->next;
  522. /* Don't translate NULL pointer for last entry */
  523. if (f->next)
  524. f->next = (struct flash_block_list *)__pa(f->next);
  525. else
  526. f->next = NULL;
  527. /* make num_blocks into the version/length field */
  528. f->num_blocks = (FLASH_BLOCK_LIST_VERSION << 56) | ((f->num_blocks+1)*16);
  529. }
  530. printk(KERN_ALERT "FLASH: flash image is %ld bytes\n", image_size);
  531. printk(KERN_ALERT "FLASH: performing flash and reboot\n");
  532. rtas_progress("Flashing \n", 0x0);
  533. rtas_progress("Please Wait... ", 0x0);
  534. printk(KERN_ALERT "FLASH: this will take several minutes. Do not power off!\n");
  535. status = rtas_call(update_token, 1, 1, NULL, rtas_block_list);
  536. switch (status) { /* should only get "bad" status */
  537. case 0:
  538. printk(KERN_ALERT "FLASH: success\n");
  539. break;
  540. case -1:
  541. printk(KERN_ALERT "FLASH: hardware error. Firmware may not be not flashed\n");
  542. break;
  543. case -3:
  544. printk(KERN_ALERT "FLASH: image is corrupt or not correct for this platform. Firmware not flashed\n");
  545. break;
  546. case -4:
  547. printk(KERN_ALERT "FLASH: flash failed when partially complete. System may not reboot\n");
  548. break;
  549. default:
  550. printk(KERN_ALERT "FLASH: unknown flash return code %d\n", status);
  551. break;
  552. }
  553. spin_unlock(&rtas_data_buf_lock);
  554. }
  555. static void remove_flash_pde(struct proc_dir_entry *dp)
  556. {
  557. if (dp) {
  558. kfree(dp->data);
  559. remove_proc_entry(dp->name, dp->parent);
  560. }
  561. }
  562. static int initialize_flash_pde_data(const char *rtas_call_name,
  563. size_t buf_size,
  564. struct proc_dir_entry *dp)
  565. {
  566. int *status;
  567. int token;
  568. dp->data = kzalloc(buf_size, GFP_KERNEL);
  569. if (dp->data == NULL)
  570. return -ENOMEM;
  571. /*
  572. * This code assumes that the status int is the first member of the
  573. * struct
  574. */
  575. status = (int *) dp->data;
  576. token = rtas_token(rtas_call_name);
  577. if (token == RTAS_UNKNOWN_SERVICE)
  578. *status = FLASH_AUTH;
  579. else
  580. *status = FLASH_NO_OP;
  581. return 0;
  582. }
  583. static struct proc_dir_entry *create_flash_pde(const char *filename,
  584. const struct file_operations *fops)
  585. {
  586. return proc_create(filename, S_IRUSR | S_IWUSR, NULL, fops);
  587. }
  588. static const struct file_operations rtas_flash_operations = {
  589. .owner = THIS_MODULE,
  590. .read = rtas_flash_read,
  591. .write = rtas_flash_write,
  592. .open = rtas_excl_open,
  593. .release = rtas_flash_release,
  594. .llseek = default_llseek,
  595. };
  596. static const struct file_operations manage_flash_operations = {
  597. .owner = THIS_MODULE,
  598. .read = manage_flash_read,
  599. .write = manage_flash_write,
  600. .open = rtas_excl_open,
  601. .release = rtas_excl_release,
  602. .llseek = default_llseek,
  603. };
  604. static const struct file_operations validate_flash_operations = {
  605. .owner = THIS_MODULE,
  606. .read = validate_flash_read,
  607. .write = validate_flash_write,
  608. .open = rtas_excl_open,
  609. .release = validate_flash_release,
  610. .llseek = default_llseek,
  611. };
  612. static int __init rtas_flash_init(void)
  613. {
  614. int rc;
  615. if (rtas_token("ibm,update-flash-64-and-reboot") ==
  616. RTAS_UNKNOWN_SERVICE) {
  617. pr_info("rtas_flash: no firmware flash support\n");
  618. return 1;
  619. }
  620. firmware_flash_pde = create_flash_pde("powerpc/rtas/"
  621. FIRMWARE_FLASH_NAME,
  622. &rtas_flash_operations);
  623. if (firmware_flash_pde == NULL) {
  624. rc = -ENOMEM;
  625. goto cleanup;
  626. }
  627. rc = initialize_flash_pde_data("ibm,update-flash-64-and-reboot",
  628. sizeof(struct rtas_update_flash_t),
  629. firmware_flash_pde);
  630. if (rc != 0)
  631. goto cleanup;
  632. firmware_update_pde = create_flash_pde("powerpc/rtas/"
  633. FIRMWARE_UPDATE_NAME,
  634. &rtas_flash_operations);
  635. if (firmware_update_pde == NULL) {
  636. rc = -ENOMEM;
  637. goto cleanup;
  638. }
  639. rc = initialize_flash_pde_data("ibm,update-flash-64-and-reboot",
  640. sizeof(struct rtas_update_flash_t),
  641. firmware_update_pde);
  642. if (rc != 0)
  643. goto cleanup;
  644. validate_pde = create_flash_pde("powerpc/rtas/" VALIDATE_FLASH_NAME,
  645. &validate_flash_operations);
  646. if (validate_pde == NULL) {
  647. rc = -ENOMEM;
  648. goto cleanup;
  649. }
  650. rc = initialize_flash_pde_data("ibm,validate-flash-image",
  651. sizeof(struct rtas_validate_flash_t),
  652. validate_pde);
  653. if (rc != 0)
  654. goto cleanup;
  655. manage_pde = create_flash_pde("powerpc/rtas/" MANAGE_FLASH_NAME,
  656. &manage_flash_operations);
  657. if (manage_pde == NULL) {
  658. rc = -ENOMEM;
  659. goto cleanup;
  660. }
  661. rc = initialize_flash_pde_data("ibm,manage-flash-image",
  662. sizeof(struct rtas_manage_flash_t),
  663. manage_pde);
  664. if (rc != 0)
  665. goto cleanup;
  666. rtas_flash_term_hook = rtas_flash_firmware;
  667. flash_block_cache = kmem_cache_create("rtas_flash_cache",
  668. RTAS_BLK_SIZE, RTAS_BLK_SIZE, 0,
  669. rtas_block_ctor);
  670. if (!flash_block_cache) {
  671. printk(KERN_ERR "%s: failed to create block cache\n",
  672. __func__);
  673. rc = -ENOMEM;
  674. goto cleanup;
  675. }
  676. return 0;
  677. cleanup:
  678. remove_flash_pde(firmware_flash_pde);
  679. remove_flash_pde(firmware_update_pde);
  680. remove_flash_pde(validate_pde);
  681. remove_flash_pde(manage_pde);
  682. return rc;
  683. }
  684. static void __exit rtas_flash_cleanup(void)
  685. {
  686. rtas_flash_term_hook = NULL;
  687. if (rtas_firmware_flash_list) {
  688. free_flash_list(rtas_firmware_flash_list);
  689. rtas_firmware_flash_list = NULL;
  690. }
  691. if (flash_block_cache)
  692. kmem_cache_destroy(flash_block_cache);
  693. remove_flash_pde(firmware_flash_pde);
  694. remove_flash_pde(firmware_update_pde);
  695. remove_flash_pde(validate_pde);
  696. remove_flash_pde(manage_pde);
  697. }
  698. module_init(rtas_flash_init);
  699. module_exit(rtas_flash_cleanup);
  700. MODULE_LICENSE("GPL");