rtas_flash.c 22 KB

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