rtas_flash.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  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 <asm/delay.h>
  20. #include <asm/uaccess.h>
  21. #include <asm/rtas.h>
  22. #include <asm/abs_addr.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. #define VALIDATE_TMP_UPDATE 0 /* Validate Return Status */
  54. #define VALIDATE_FLASH_AUTH 1 /* Validate Return Status */
  55. #define VALIDATE_INVALID_IMG 2 /* Validate Return Status */
  56. #define VALIDATE_CUR_UNKNOWN 3 /* Validate Return Status */
  57. #define VALIDATE_TMP_COMMIT_DL 4 /* Validate Return Status */
  58. #define VALIDATE_TMP_COMMIT 5 /* Validate Return Status */
  59. #define VALIDATE_TMP_UPDATE_DL 6 /* Validate Return Status */
  60. /* ibm,manage-flash-image operation tokens */
  61. #define RTAS_REJECT_TMP_IMG 0
  62. #define RTAS_COMMIT_TMP_IMG 1
  63. /* Array sizes */
  64. #define VALIDATE_BUF_SIZE 4096
  65. #define RTAS_MSG_MAXLEN 64
  66. /* Quirk - RTAS requires 4k list length and block size */
  67. #define RTAS_BLKLIST_LENGTH 4096
  68. #define RTAS_BLK_SIZE 4096
  69. struct flash_block {
  70. char *data;
  71. unsigned long length;
  72. };
  73. /* This struct is very similar but not identical to
  74. * that needed by the rtas flash update.
  75. * All we need to do for rtas is rewrite num_blocks
  76. * into a version/length and translate the pointers
  77. * to absolute.
  78. */
  79. #define FLASH_BLOCKS_PER_NODE ((RTAS_BLKLIST_LENGTH - 16) / sizeof(struct flash_block))
  80. struct flash_block_list {
  81. unsigned long num_blocks;
  82. struct flash_block_list *next;
  83. struct flash_block blocks[FLASH_BLOCKS_PER_NODE];
  84. };
  85. static struct flash_block_list *rtas_firmware_flash_list;
  86. /* Use slab cache to guarantee 4k alignment */
  87. static struct kmem_cache *flash_block_cache = NULL;
  88. #define FLASH_BLOCK_LIST_VERSION (1UL)
  89. /* Local copy of the flash block list.
  90. * We only allow one open of the flash proc file and create this
  91. * list as we go. The rtas_firmware_flash_list varable will be
  92. * set once the data is fully read.
  93. *
  94. * For convenience as we build the list we use virtual addrs,
  95. * we do not fill in the version number, and the length field
  96. * is treated as the number of entries currently in the block
  97. * (i.e. not a byte count). This is all fixed when calling
  98. * the flash routine.
  99. */
  100. /* Status int must be first member of struct */
  101. struct rtas_update_flash_t
  102. {
  103. int status; /* Flash update status */
  104. struct flash_block_list *flist; /* Local copy of flash block list */
  105. };
  106. /* Status int must be first member of struct */
  107. struct rtas_manage_flash_t
  108. {
  109. int status; /* Returned status */
  110. unsigned int op; /* Reject or commit image */
  111. };
  112. /* Status int must be first member of struct */
  113. struct rtas_validate_flash_t
  114. {
  115. int status; /* Returned status */
  116. char buf[VALIDATE_BUF_SIZE]; /* Candidate image buffer */
  117. unsigned int buf_size; /* Size of image buf */
  118. unsigned int update_results; /* Update results token */
  119. };
  120. static DEFINE_SPINLOCK(flash_file_open_lock);
  121. static struct proc_dir_entry *firmware_flash_pde;
  122. static struct proc_dir_entry *firmware_update_pde;
  123. static struct proc_dir_entry *validate_pde;
  124. static struct proc_dir_entry *manage_pde;
  125. /* Do simple sanity checks on the flash image. */
  126. static int flash_list_valid(struct flash_block_list *flist)
  127. {
  128. struct flash_block_list *f;
  129. int i;
  130. unsigned long block_size, image_size;
  131. /* Paranoid self test here. We also collect the image size. */
  132. image_size = 0;
  133. for (f = flist; f; f = f->next) {
  134. for (i = 0; i < f->num_blocks; i++) {
  135. if (f->blocks[i].data == NULL) {
  136. return FLASH_IMG_NULL_DATA;
  137. }
  138. block_size = f->blocks[i].length;
  139. if (block_size <= 0 || block_size > RTAS_BLK_SIZE) {
  140. return FLASH_IMG_BAD_LEN;
  141. }
  142. image_size += block_size;
  143. }
  144. }
  145. if (image_size < (256 << 10)) {
  146. if (image_size < 2)
  147. return FLASH_NO_OP;
  148. }
  149. printk(KERN_INFO "FLASH: flash image with %ld bytes stored for hardware flash on reboot\n", image_size);
  150. return FLASH_IMG_READY;
  151. }
  152. static void free_flash_list(struct flash_block_list *f)
  153. {
  154. struct flash_block_list *next;
  155. int i;
  156. while (f) {
  157. for (i = 0; i < f->num_blocks; i++)
  158. kmem_cache_free(flash_block_cache, f->blocks[i].data);
  159. next = f->next;
  160. kmem_cache_free(flash_block_cache, f);
  161. f = next;
  162. }
  163. }
  164. static int rtas_flash_release(struct inode *inode, struct file *file)
  165. {
  166. struct proc_dir_entry *dp = PDE(file->f_path.dentry->d_inode);
  167. struct rtas_update_flash_t *uf;
  168. uf = (struct rtas_update_flash_t *) dp->data;
  169. if (uf->flist) {
  170. /* File was opened in write mode for a new flash attempt */
  171. /* Clear saved list */
  172. if (rtas_firmware_flash_list) {
  173. free_flash_list(rtas_firmware_flash_list);
  174. rtas_firmware_flash_list = NULL;
  175. }
  176. if (uf->status != FLASH_AUTH)
  177. uf->status = flash_list_valid(uf->flist);
  178. if (uf->status == FLASH_IMG_READY)
  179. rtas_firmware_flash_list = uf->flist;
  180. else
  181. free_flash_list(uf->flist);
  182. uf->flist = NULL;
  183. }
  184. atomic_dec(&dp->count);
  185. return 0;
  186. }
  187. static void get_flash_status_msg(int status, char *buf)
  188. {
  189. char *msg;
  190. switch (status) {
  191. case FLASH_AUTH:
  192. msg = "error: this partition does not have service authority\n";
  193. break;
  194. case FLASH_NO_OP:
  195. msg = "info: no firmware image for flash\n";
  196. break;
  197. case FLASH_IMG_SHORT:
  198. msg = "error: flash image short\n";
  199. break;
  200. case FLASH_IMG_BAD_LEN:
  201. msg = "error: internal error bad length\n";
  202. break;
  203. case FLASH_IMG_NULL_DATA:
  204. msg = "error: internal error null data\n";
  205. break;
  206. case FLASH_IMG_READY:
  207. msg = "ready: firmware image ready for flash on reboot\n";
  208. break;
  209. default:
  210. sprintf(buf, "error: unexpected status value %d\n", status);
  211. return;
  212. }
  213. strcpy(buf, msg);
  214. }
  215. /* Reading the proc file will show status (not the firmware contents) */
  216. static ssize_t rtas_flash_read(struct file *file, char __user *buf,
  217. size_t count, loff_t *ppos)
  218. {
  219. struct proc_dir_entry *dp = PDE(file->f_path.dentry->d_inode);
  220. struct rtas_update_flash_t *uf;
  221. char msg[RTAS_MSG_MAXLEN];
  222. int msglen;
  223. uf = (struct rtas_update_flash_t *) 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. msglen = strlen(msg);
  230. if (msglen > count)
  231. msglen = count;
  232. if (ppos && *ppos != 0)
  233. return 0; /* be cheap */
  234. if (!access_ok(VERIFY_WRITE, buf, msglen))
  235. return -EINVAL;
  236. if (copy_to_user(buf, msg, msglen))
  237. return -EFAULT;
  238. if (ppos)
  239. *ppos = msglen;
  240. return msglen;
  241. }
  242. /* constructor for flash_block_cache */
  243. void rtas_block_ctor(void *ptr)
  244. {
  245. memset(ptr, 0, RTAS_BLK_SIZE);
  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->f_path.dentry->d_inode);
  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_alloc(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_alloc(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_alloc(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->f_path.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_path.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. int update_results;
  382. s32 rc;
  383. rc = 0;
  384. do {
  385. spin_lock(&rtas_data_buf_lock);
  386. memcpy(rtas_data_buf, args_buf->buf, VALIDATE_BUF_SIZE);
  387. rc = rtas_call(token, 2, 2, &update_results,
  388. (u32) __pa(rtas_data_buf), args_buf->buf_size);
  389. memcpy(args_buf->buf, rtas_data_buf, VALIDATE_BUF_SIZE);
  390. spin_unlock(&rtas_data_buf_lock);
  391. } while (rtas_busy_delay(rc));
  392. args_buf->status = rc;
  393. args_buf->update_results = update_results;
  394. }
  395. static int get_validate_flash_msg(struct rtas_validate_flash_t *args_buf,
  396. char *msg)
  397. {
  398. int n;
  399. if (args_buf->status >= VALIDATE_TMP_UPDATE) {
  400. n = sprintf(msg, "%d\n", args_buf->update_results);
  401. if ((args_buf->update_results >= VALIDATE_CUR_UNKNOWN) ||
  402. (args_buf->update_results == VALIDATE_TMP_UPDATE))
  403. n += sprintf(msg + n, "%s\n", args_buf->buf);
  404. } else {
  405. n = sprintf(msg, "%d\n", args_buf->status);
  406. }
  407. return n;
  408. }
  409. static ssize_t validate_flash_read(struct file *file, char __user *buf,
  410. size_t count, loff_t *ppos)
  411. {
  412. struct proc_dir_entry *dp = PDE(file->f_path.dentry->d_inode);
  413. struct rtas_validate_flash_t *args_buf;
  414. char msg[RTAS_MSG_MAXLEN];
  415. int msglen;
  416. args_buf = (struct rtas_validate_flash_t *) dp->data;
  417. if (ppos && *ppos != 0)
  418. return 0; /* be cheap */
  419. msglen = get_validate_flash_msg(args_buf, msg);
  420. if (msglen > count)
  421. msglen = count;
  422. if (!access_ok(VERIFY_WRITE, buf, msglen))
  423. return -EINVAL;
  424. if (copy_to_user(buf, msg, msglen))
  425. return -EFAULT;
  426. if (ppos)
  427. *ppos = msglen;
  428. return msglen;
  429. }
  430. static ssize_t validate_flash_write(struct file *file, const char __user *buf,
  431. size_t count, loff_t *off)
  432. {
  433. struct proc_dir_entry *dp = PDE(file->f_path.dentry->d_inode);
  434. struct rtas_validate_flash_t *args_buf;
  435. int rc;
  436. args_buf = (struct rtas_validate_flash_t *) dp->data;
  437. if (dp->data == NULL) {
  438. dp->data = kmalloc(sizeof(struct rtas_validate_flash_t),
  439. GFP_KERNEL);
  440. if (dp->data == NULL)
  441. return -ENOMEM;
  442. }
  443. /* We are only interested in the first 4K of the
  444. * candidate image */
  445. if ((*off >= VALIDATE_BUF_SIZE) ||
  446. (args_buf->status == VALIDATE_AUTH)) {
  447. *off += count;
  448. return count;
  449. }
  450. if (*off + count >= VALIDATE_BUF_SIZE) {
  451. count = VALIDATE_BUF_SIZE - *off;
  452. args_buf->status = VALIDATE_READY;
  453. } else {
  454. args_buf->status = VALIDATE_INCOMPLETE;
  455. }
  456. if (!access_ok(VERIFY_READ, buf, count)) {
  457. rc = -EFAULT;
  458. goto done;
  459. }
  460. if (copy_from_user(args_buf->buf + *off, buf, count)) {
  461. rc = -EFAULT;
  462. goto done;
  463. }
  464. *off += count;
  465. rc = count;
  466. done:
  467. if (rc < 0) {
  468. kfree(dp->data);
  469. dp->data = NULL;
  470. }
  471. return rc;
  472. }
  473. static int validate_flash_release(struct inode *inode, struct file *file)
  474. {
  475. struct proc_dir_entry *dp = PDE(file->f_path.dentry->d_inode);
  476. struct rtas_validate_flash_t *args_buf;
  477. args_buf = (struct rtas_validate_flash_t *) dp->data;
  478. if (args_buf->status == VALIDATE_READY) {
  479. args_buf->buf_size = VALIDATE_BUF_SIZE;
  480. validate_flash(args_buf);
  481. }
  482. /* The matching atomic_inc was in rtas_excl_open() */
  483. atomic_dec(&dp->count);
  484. return 0;
  485. }
  486. static void rtas_flash_firmware(int reboot_type)
  487. {
  488. unsigned long image_size;
  489. struct flash_block_list *f, *next, *flist;
  490. unsigned long rtas_block_list;
  491. int i, status, update_token;
  492. if (rtas_firmware_flash_list == NULL)
  493. return; /* nothing to do */
  494. if (reboot_type != SYS_RESTART) {
  495. printk(KERN_ALERT "FLASH: firmware flash requires a reboot\n");
  496. printk(KERN_ALERT "FLASH: the firmware image will NOT be flashed\n");
  497. return;
  498. }
  499. update_token = rtas_token("ibm,update-flash-64-and-reboot");
  500. if (update_token == RTAS_UNKNOWN_SERVICE) {
  501. printk(KERN_ALERT "FLASH: ibm,update-flash-64-and-reboot "
  502. "is not available -- not a service partition?\n");
  503. printk(KERN_ALERT "FLASH: firmware will not be flashed\n");
  504. return;
  505. }
  506. /*
  507. * NOTE: the "first" block must be under 4GB, so we create
  508. * an entry with no data blocks in the reserved buffer in
  509. * the kernel data segment.
  510. */
  511. spin_lock(&rtas_data_buf_lock);
  512. flist = (struct flash_block_list *)&rtas_data_buf[0];
  513. flist->num_blocks = 0;
  514. flist->next = rtas_firmware_flash_list;
  515. rtas_block_list = virt_to_abs(flist);
  516. if (rtas_block_list >= 4UL*1024*1024*1024) {
  517. printk(KERN_ALERT "FLASH: kernel bug...flash list header addr above 4GB\n");
  518. spin_unlock(&rtas_data_buf_lock);
  519. return;
  520. }
  521. printk(KERN_ALERT "FLASH: preparing saved firmware image for flash\n");
  522. /* Update the block_list in place. */
  523. rtas_firmware_flash_list = NULL; /* too hard to backout on error */
  524. image_size = 0;
  525. for (f = flist; f; f = next) {
  526. /* Translate data addrs to absolute */
  527. for (i = 0; i < f->num_blocks; i++) {
  528. f->blocks[i].data = (char *)virt_to_abs(f->blocks[i].data);
  529. image_size += f->blocks[i].length;
  530. }
  531. next = f->next;
  532. /* Don't translate NULL pointer for last entry */
  533. if (f->next)
  534. f->next = (struct flash_block_list *)virt_to_abs(f->next);
  535. else
  536. f->next = NULL;
  537. /* make num_blocks into the version/length field */
  538. f->num_blocks = (FLASH_BLOCK_LIST_VERSION << 56) | ((f->num_blocks+1)*16);
  539. }
  540. printk(KERN_ALERT "FLASH: flash image is %ld bytes\n", image_size);
  541. printk(KERN_ALERT "FLASH: performing flash and reboot\n");
  542. rtas_progress("Flashing \n", 0x0);
  543. rtas_progress("Please Wait... ", 0x0);
  544. printk(KERN_ALERT "FLASH: this will take several minutes. Do not power off!\n");
  545. status = rtas_call(update_token, 1, 1, NULL, rtas_block_list);
  546. switch (status) { /* should only get "bad" status */
  547. case 0:
  548. printk(KERN_ALERT "FLASH: success\n");
  549. break;
  550. case -1:
  551. printk(KERN_ALERT "FLASH: hardware error. Firmware may not be not flashed\n");
  552. break;
  553. case -3:
  554. printk(KERN_ALERT "FLASH: image is corrupt or not correct for this platform. Firmware not flashed\n");
  555. break;
  556. case -4:
  557. printk(KERN_ALERT "FLASH: flash failed when partially complete. System may not reboot\n");
  558. break;
  559. default:
  560. printk(KERN_ALERT "FLASH: unknown flash return code %d\n", status);
  561. break;
  562. }
  563. spin_unlock(&rtas_data_buf_lock);
  564. }
  565. static void remove_flash_pde(struct proc_dir_entry *dp)
  566. {
  567. if (dp) {
  568. kfree(dp->data);
  569. remove_proc_entry(dp->name, dp->parent);
  570. }
  571. }
  572. static int initialize_flash_pde_data(const char *rtas_call_name,
  573. size_t buf_size,
  574. struct proc_dir_entry *dp)
  575. {
  576. int *status;
  577. int token;
  578. dp->data = kzalloc(buf_size, GFP_KERNEL);
  579. if (dp->data == NULL) {
  580. remove_flash_pde(dp);
  581. return -ENOMEM;
  582. }
  583. /*
  584. * This code assumes that the status int is the first member of the
  585. * struct
  586. */
  587. status = (int *) dp->data;
  588. token = rtas_token(rtas_call_name);
  589. if (token == RTAS_UNKNOWN_SERVICE)
  590. *status = FLASH_AUTH;
  591. else
  592. *status = FLASH_NO_OP;
  593. return 0;
  594. }
  595. static struct proc_dir_entry *create_flash_pde(const char *filename,
  596. const struct file_operations *fops)
  597. {
  598. return proc_create(filename, S_IRUSR | S_IWUSR, NULL, fops);
  599. }
  600. static const struct file_operations rtas_flash_operations = {
  601. .owner = THIS_MODULE,
  602. .read = rtas_flash_read,
  603. .write = rtas_flash_write,
  604. .open = rtas_excl_open,
  605. .release = rtas_flash_release,
  606. };
  607. static const struct file_operations manage_flash_operations = {
  608. .owner = THIS_MODULE,
  609. .read = manage_flash_read,
  610. .write = manage_flash_write,
  611. .open = rtas_excl_open,
  612. .release = rtas_excl_release,
  613. };
  614. static const struct file_operations validate_flash_operations = {
  615. .owner = THIS_MODULE,
  616. .read = validate_flash_read,
  617. .write = validate_flash_write,
  618. .open = rtas_excl_open,
  619. .release = validate_flash_release,
  620. };
  621. static int __init rtas_flash_init(void)
  622. {
  623. int rc;
  624. if (rtas_token("ibm,update-flash-64-and-reboot") ==
  625. RTAS_UNKNOWN_SERVICE) {
  626. printk(KERN_ERR "rtas_flash: no firmware flash support\n");
  627. return 1;
  628. }
  629. firmware_flash_pde = create_flash_pde("powerpc/rtas/"
  630. FIRMWARE_FLASH_NAME,
  631. &rtas_flash_operations);
  632. if (firmware_flash_pde == NULL) {
  633. rc = -ENOMEM;
  634. goto cleanup;
  635. }
  636. rc = initialize_flash_pde_data("ibm,update-flash-64-and-reboot",
  637. sizeof(struct rtas_update_flash_t),
  638. firmware_flash_pde);
  639. if (rc != 0)
  640. goto cleanup;
  641. firmware_update_pde = create_flash_pde("powerpc/rtas/"
  642. FIRMWARE_UPDATE_NAME,
  643. &rtas_flash_operations);
  644. if (firmware_update_pde == NULL) {
  645. rc = -ENOMEM;
  646. goto cleanup;
  647. }
  648. rc = initialize_flash_pde_data("ibm,update-flash-64-and-reboot",
  649. sizeof(struct rtas_update_flash_t),
  650. firmware_update_pde);
  651. if (rc != 0)
  652. goto cleanup;
  653. validate_pde = create_flash_pde("powerpc/rtas/" VALIDATE_FLASH_NAME,
  654. &validate_flash_operations);
  655. if (validate_pde == NULL) {
  656. rc = -ENOMEM;
  657. goto cleanup;
  658. }
  659. rc = initialize_flash_pde_data("ibm,validate-flash-image",
  660. sizeof(struct rtas_validate_flash_t),
  661. validate_pde);
  662. if (rc != 0)
  663. goto cleanup;
  664. manage_pde = create_flash_pde("powerpc/rtas/" MANAGE_FLASH_NAME,
  665. &manage_flash_operations);
  666. if (manage_pde == NULL) {
  667. rc = -ENOMEM;
  668. goto cleanup;
  669. }
  670. rc = initialize_flash_pde_data("ibm,manage-flash-image",
  671. sizeof(struct rtas_manage_flash_t),
  672. manage_pde);
  673. if (rc != 0)
  674. goto cleanup;
  675. rtas_flash_term_hook = rtas_flash_firmware;
  676. flash_block_cache = kmem_cache_create("rtas_flash_cache",
  677. RTAS_BLK_SIZE, RTAS_BLK_SIZE, 0,
  678. rtas_block_ctor);
  679. if (!flash_block_cache) {
  680. printk(KERN_ERR "%s: failed to create block cache\n",
  681. __func__);
  682. rc = -ENOMEM;
  683. goto cleanup;
  684. }
  685. return 0;
  686. cleanup:
  687. remove_flash_pde(firmware_flash_pde);
  688. remove_flash_pde(firmware_update_pde);
  689. remove_flash_pde(validate_pde);
  690. remove_flash_pde(manage_pde);
  691. return rc;
  692. }
  693. static void __exit rtas_flash_cleanup(void)
  694. {
  695. rtas_flash_term_hook = NULL;
  696. if (flash_block_cache)
  697. kmem_cache_destroy(flash_block_cache);
  698. remove_flash_pde(firmware_flash_pde);
  699. remove_flash_pde(firmware_update_pde);
  700. remove_flash_pde(validate_pde);
  701. remove_flash_pde(manage_pde);
  702. }
  703. module_init(rtas_flash_init);
  704. module_exit(rtas_flash_cleanup);
  705. MODULE_LICENSE("GPL");