rtas_flash.c 22 KB

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