rtas_flash.c 22 KB

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