rtas_flash.c 22 KB

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