rtas_flash.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  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. uf = dp->data;
  223. if (!strcmp(dp->name, FIRMWARE_FLASH_NAME)) {
  224. get_flash_status_msg(uf->status, msg);
  225. } else { /* FIRMWARE_UPDATE_NAME */
  226. sprintf(msg, "%d\n", uf->status);
  227. }
  228. return simple_read_from_buffer(buf, count, ppos, msg, strlen(msg));
  229. }
  230. /* constructor for flash_block_cache */
  231. void rtas_block_ctor(void *ptr)
  232. {
  233. memset(ptr, 0, RTAS_BLK_SIZE);
  234. }
  235. /* We could be much more efficient here. But to keep this function
  236. * simple we allocate a page to the block list no matter how small the
  237. * count is. If the system is low on memory it will be just as well
  238. * that we fail....
  239. */
  240. static ssize_t rtas_flash_write(struct file *file, const char __user *buffer,
  241. size_t count, loff_t *off)
  242. {
  243. struct proc_dir_entry *dp = PDE(file->f_path.dentry->d_inode);
  244. struct rtas_update_flash_t *uf;
  245. char *p;
  246. int next_free;
  247. struct flash_block_list *fl;
  248. uf = (struct rtas_update_flash_t *) dp->data;
  249. if (uf->status == FLASH_AUTH || count == 0)
  250. return count; /* discard data */
  251. /* In the case that the image is not ready for flashing, the memory
  252. * allocated for the block list will be freed upon the release of the
  253. * proc file
  254. */
  255. if (uf->flist == NULL) {
  256. uf->flist = kmem_cache_alloc(flash_block_cache, GFP_KERNEL);
  257. if (!uf->flist)
  258. return -ENOMEM;
  259. }
  260. fl = uf->flist;
  261. while (fl->next)
  262. fl = fl->next; /* seek to last block_list for append */
  263. next_free = fl->num_blocks;
  264. if (next_free == FLASH_BLOCKS_PER_NODE) {
  265. /* Need to allocate another block_list */
  266. fl->next = kmem_cache_alloc(flash_block_cache, GFP_KERNEL);
  267. if (!fl->next)
  268. return -ENOMEM;
  269. fl = fl->next;
  270. next_free = 0;
  271. }
  272. if (count > RTAS_BLK_SIZE)
  273. count = RTAS_BLK_SIZE;
  274. p = kmem_cache_alloc(flash_block_cache, GFP_KERNEL);
  275. if (!p)
  276. return -ENOMEM;
  277. if(copy_from_user(p, buffer, count)) {
  278. kmem_cache_free(flash_block_cache, p);
  279. return -EFAULT;
  280. }
  281. fl->blocks[next_free].data = p;
  282. fl->blocks[next_free].length = count;
  283. fl->num_blocks++;
  284. return count;
  285. }
  286. static int rtas_excl_open(struct inode *inode, struct file *file)
  287. {
  288. struct proc_dir_entry *dp = PDE(inode);
  289. /* Enforce exclusive open with use count of PDE */
  290. spin_lock(&flash_file_open_lock);
  291. if (atomic_read(&dp->count) > 2) {
  292. spin_unlock(&flash_file_open_lock);
  293. return -EBUSY;
  294. }
  295. atomic_inc(&dp->count);
  296. spin_unlock(&flash_file_open_lock);
  297. return 0;
  298. }
  299. static int rtas_excl_release(struct inode *inode, struct file *file)
  300. {
  301. struct proc_dir_entry *dp = PDE(inode);
  302. atomic_dec(&dp->count);
  303. return 0;
  304. }
  305. static void manage_flash(struct rtas_manage_flash_t *args_buf)
  306. {
  307. s32 rc;
  308. do {
  309. rc = rtas_call(rtas_token("ibm,manage-flash-image"), 1,
  310. 1, NULL, args_buf->op);
  311. } while (rtas_busy_delay(rc));
  312. args_buf->status = rc;
  313. }
  314. static ssize_t manage_flash_read(struct file *file, char __user *buf,
  315. size_t count, loff_t *ppos)
  316. {
  317. struct proc_dir_entry *dp = PDE(file->f_path.dentry->d_inode);
  318. struct rtas_manage_flash_t *args_buf;
  319. char msg[RTAS_MSG_MAXLEN];
  320. int msglen;
  321. args_buf = dp->data;
  322. if (args_buf == NULL)
  323. return 0;
  324. msglen = sprintf(msg, "%d\n", args_buf->status);
  325. return simple_read_from_buffer(buf, count, ppos, msg, msglen);
  326. }
  327. static ssize_t manage_flash_write(struct file *file, const char __user *buf,
  328. size_t count, loff_t *off)
  329. {
  330. struct proc_dir_entry *dp = PDE(file->f_path.dentry->d_inode);
  331. struct rtas_manage_flash_t *args_buf;
  332. const char reject_str[] = "0";
  333. const char commit_str[] = "1";
  334. char stkbuf[10];
  335. int op;
  336. args_buf = (struct rtas_manage_flash_t *) dp->data;
  337. if ((args_buf->status == MANAGE_AUTH) || (count == 0))
  338. return count;
  339. op = -1;
  340. if (buf) {
  341. if (count > 9) count = 9;
  342. if (copy_from_user (stkbuf, buf, count)) {
  343. return -EFAULT;
  344. }
  345. if (strncmp(stkbuf, reject_str, strlen(reject_str)) == 0)
  346. op = RTAS_REJECT_TMP_IMG;
  347. else if (strncmp(stkbuf, commit_str, strlen(commit_str)) == 0)
  348. op = RTAS_COMMIT_TMP_IMG;
  349. }
  350. if (op == -1) /* buf is empty, or contains invalid string */
  351. return -EINVAL;
  352. args_buf->op = op;
  353. manage_flash(args_buf);
  354. return count;
  355. }
  356. static void validate_flash(struct rtas_validate_flash_t *args_buf)
  357. {
  358. int token = rtas_token("ibm,validate-flash-image");
  359. int update_results;
  360. s32 rc;
  361. rc = 0;
  362. do {
  363. spin_lock(&rtas_data_buf_lock);
  364. memcpy(rtas_data_buf, args_buf->buf, VALIDATE_BUF_SIZE);
  365. rc = rtas_call(token, 2, 2, &update_results,
  366. (u32) __pa(rtas_data_buf), args_buf->buf_size);
  367. memcpy(args_buf->buf, rtas_data_buf, VALIDATE_BUF_SIZE);
  368. spin_unlock(&rtas_data_buf_lock);
  369. } while (rtas_busy_delay(rc));
  370. args_buf->status = rc;
  371. args_buf->update_results = update_results;
  372. }
  373. static int get_validate_flash_msg(struct rtas_validate_flash_t *args_buf,
  374. char *msg)
  375. {
  376. int n;
  377. if (args_buf->status >= VALIDATE_TMP_UPDATE) {
  378. n = sprintf(msg, "%d\n", args_buf->update_results);
  379. if ((args_buf->update_results >= VALIDATE_CUR_UNKNOWN) ||
  380. (args_buf->update_results == VALIDATE_TMP_UPDATE))
  381. n += sprintf(msg + n, "%s\n", args_buf->buf);
  382. } else {
  383. n = sprintf(msg, "%d\n", args_buf->status);
  384. }
  385. return n;
  386. }
  387. static ssize_t validate_flash_read(struct file *file, char __user *buf,
  388. size_t count, loff_t *ppos)
  389. {
  390. struct proc_dir_entry *dp = PDE(file->f_path.dentry->d_inode);
  391. struct rtas_validate_flash_t *args_buf;
  392. char msg[RTAS_MSG_MAXLEN];
  393. int msglen;
  394. args_buf = dp->data;
  395. msglen = get_validate_flash_msg(args_buf, msg);
  396. return simple_read_from_buffer(buf, count, ppos, msg, msglen);
  397. }
  398. static ssize_t validate_flash_write(struct file *file, const char __user *buf,
  399. size_t count, loff_t *off)
  400. {
  401. struct proc_dir_entry *dp = PDE(file->f_path.dentry->d_inode);
  402. struct rtas_validate_flash_t *args_buf;
  403. int rc;
  404. args_buf = (struct rtas_validate_flash_t *) dp->data;
  405. if (dp->data == NULL) {
  406. dp->data = kmalloc(sizeof(struct rtas_validate_flash_t),
  407. GFP_KERNEL);
  408. if (dp->data == NULL)
  409. return -ENOMEM;
  410. }
  411. /* We are only interested in the first 4K of the
  412. * candidate image */
  413. if ((*off >= VALIDATE_BUF_SIZE) ||
  414. (args_buf->status == VALIDATE_AUTH)) {
  415. *off += count;
  416. return count;
  417. }
  418. if (*off + count >= VALIDATE_BUF_SIZE) {
  419. count = VALIDATE_BUF_SIZE - *off;
  420. args_buf->status = VALIDATE_READY;
  421. } else {
  422. args_buf->status = VALIDATE_INCOMPLETE;
  423. }
  424. if (!access_ok(VERIFY_READ, buf, count)) {
  425. rc = -EFAULT;
  426. goto done;
  427. }
  428. if (copy_from_user(args_buf->buf + *off, buf, count)) {
  429. rc = -EFAULT;
  430. goto done;
  431. }
  432. *off += count;
  433. rc = count;
  434. done:
  435. if (rc < 0) {
  436. kfree(dp->data);
  437. dp->data = NULL;
  438. }
  439. return rc;
  440. }
  441. static int validate_flash_release(struct inode *inode, struct file *file)
  442. {
  443. struct proc_dir_entry *dp = PDE(file->f_path.dentry->d_inode);
  444. struct rtas_validate_flash_t *args_buf;
  445. args_buf = (struct rtas_validate_flash_t *) dp->data;
  446. if (args_buf->status == VALIDATE_READY) {
  447. args_buf->buf_size = VALIDATE_BUF_SIZE;
  448. validate_flash(args_buf);
  449. }
  450. /* The matching atomic_inc was in rtas_excl_open() */
  451. atomic_dec(&dp->count);
  452. return 0;
  453. }
  454. static void rtas_flash_firmware(int reboot_type)
  455. {
  456. unsigned long image_size;
  457. struct flash_block_list *f, *next, *flist;
  458. unsigned long rtas_block_list;
  459. int i, status, update_token;
  460. if (rtas_firmware_flash_list == NULL)
  461. return; /* nothing to do */
  462. if (reboot_type != SYS_RESTART) {
  463. printk(KERN_ALERT "FLASH: firmware flash requires a reboot\n");
  464. printk(KERN_ALERT "FLASH: the firmware image will NOT be flashed\n");
  465. return;
  466. }
  467. update_token = rtas_token("ibm,update-flash-64-and-reboot");
  468. if (update_token == RTAS_UNKNOWN_SERVICE) {
  469. printk(KERN_ALERT "FLASH: ibm,update-flash-64-and-reboot "
  470. "is not available -- not a service partition?\n");
  471. printk(KERN_ALERT "FLASH: firmware will not be flashed\n");
  472. return;
  473. }
  474. /*
  475. * NOTE: the "first" block must be under 4GB, so we create
  476. * an entry with no data blocks in the reserved buffer in
  477. * the kernel data segment.
  478. */
  479. spin_lock(&rtas_data_buf_lock);
  480. flist = (struct flash_block_list *)&rtas_data_buf[0];
  481. flist->num_blocks = 0;
  482. flist->next = rtas_firmware_flash_list;
  483. rtas_block_list = virt_to_abs(flist);
  484. if (rtas_block_list >= 4UL*1024*1024*1024) {
  485. printk(KERN_ALERT "FLASH: kernel bug...flash list header addr above 4GB\n");
  486. spin_unlock(&rtas_data_buf_lock);
  487. return;
  488. }
  489. printk(KERN_ALERT "FLASH: preparing saved firmware image for flash\n");
  490. /* Update the block_list in place. */
  491. rtas_firmware_flash_list = NULL; /* too hard to backout on error */
  492. image_size = 0;
  493. for (f = flist; f; f = next) {
  494. /* Translate data addrs to absolute */
  495. for (i = 0; i < f->num_blocks; i++) {
  496. f->blocks[i].data = (char *)virt_to_abs(f->blocks[i].data);
  497. image_size += f->blocks[i].length;
  498. }
  499. next = f->next;
  500. /* Don't translate NULL pointer for last entry */
  501. if (f->next)
  502. f->next = (struct flash_block_list *)virt_to_abs(f->next);
  503. else
  504. f->next = NULL;
  505. /* make num_blocks into the version/length field */
  506. f->num_blocks = (FLASH_BLOCK_LIST_VERSION << 56) | ((f->num_blocks+1)*16);
  507. }
  508. printk(KERN_ALERT "FLASH: flash image is %ld bytes\n", image_size);
  509. printk(KERN_ALERT "FLASH: performing flash and reboot\n");
  510. rtas_progress("Flashing \n", 0x0);
  511. rtas_progress("Please Wait... ", 0x0);
  512. printk(KERN_ALERT "FLASH: this will take several minutes. Do not power off!\n");
  513. status = rtas_call(update_token, 1, 1, NULL, rtas_block_list);
  514. switch (status) { /* should only get "bad" status */
  515. case 0:
  516. printk(KERN_ALERT "FLASH: success\n");
  517. break;
  518. case -1:
  519. printk(KERN_ALERT "FLASH: hardware error. Firmware may not be not flashed\n");
  520. break;
  521. case -3:
  522. printk(KERN_ALERT "FLASH: image is corrupt or not correct for this platform. Firmware not flashed\n");
  523. break;
  524. case -4:
  525. printk(KERN_ALERT "FLASH: flash failed when partially complete. System may not reboot\n");
  526. break;
  527. default:
  528. printk(KERN_ALERT "FLASH: unknown flash return code %d\n", status);
  529. break;
  530. }
  531. spin_unlock(&rtas_data_buf_lock);
  532. }
  533. static void remove_flash_pde(struct proc_dir_entry *dp)
  534. {
  535. if (dp) {
  536. kfree(dp->data);
  537. remove_proc_entry(dp->name, dp->parent);
  538. }
  539. }
  540. static int initialize_flash_pde_data(const char *rtas_call_name,
  541. size_t buf_size,
  542. struct proc_dir_entry *dp)
  543. {
  544. int *status;
  545. int token;
  546. dp->data = kzalloc(buf_size, GFP_KERNEL);
  547. if (dp->data == NULL) {
  548. remove_flash_pde(dp);
  549. return -ENOMEM;
  550. }
  551. /*
  552. * This code assumes that the status int is the first member of the
  553. * struct
  554. */
  555. status = (int *) dp->data;
  556. token = rtas_token(rtas_call_name);
  557. if (token == RTAS_UNKNOWN_SERVICE)
  558. *status = FLASH_AUTH;
  559. else
  560. *status = FLASH_NO_OP;
  561. return 0;
  562. }
  563. static struct proc_dir_entry *create_flash_pde(const char *filename,
  564. const struct file_operations *fops)
  565. {
  566. return proc_create(filename, S_IRUSR | S_IWUSR, NULL, fops);
  567. }
  568. static const struct file_operations rtas_flash_operations = {
  569. .owner = THIS_MODULE,
  570. .read = rtas_flash_read,
  571. .write = rtas_flash_write,
  572. .open = rtas_excl_open,
  573. .release = rtas_flash_release,
  574. .llseek = default_llseek,
  575. };
  576. static const struct file_operations manage_flash_operations = {
  577. .owner = THIS_MODULE,
  578. .read = manage_flash_read,
  579. .write = manage_flash_write,
  580. .open = rtas_excl_open,
  581. .release = rtas_excl_release,
  582. .llseek = default_llseek,
  583. };
  584. static const struct file_operations validate_flash_operations = {
  585. .owner = THIS_MODULE,
  586. .read = validate_flash_read,
  587. .write = validate_flash_write,
  588. .open = rtas_excl_open,
  589. .release = validate_flash_release,
  590. .llseek = default_llseek,
  591. };
  592. static int __init rtas_flash_init(void)
  593. {
  594. int rc;
  595. if (rtas_token("ibm,update-flash-64-and-reboot") ==
  596. RTAS_UNKNOWN_SERVICE) {
  597. printk(KERN_ERR "rtas_flash: no firmware flash support\n");
  598. return 1;
  599. }
  600. firmware_flash_pde = create_flash_pde("powerpc/rtas/"
  601. FIRMWARE_FLASH_NAME,
  602. &rtas_flash_operations);
  603. if (firmware_flash_pde == NULL) {
  604. rc = -ENOMEM;
  605. goto cleanup;
  606. }
  607. rc = initialize_flash_pde_data("ibm,update-flash-64-and-reboot",
  608. sizeof(struct rtas_update_flash_t),
  609. firmware_flash_pde);
  610. if (rc != 0)
  611. goto cleanup;
  612. firmware_update_pde = create_flash_pde("powerpc/rtas/"
  613. FIRMWARE_UPDATE_NAME,
  614. &rtas_flash_operations);
  615. if (firmware_update_pde == NULL) {
  616. rc = -ENOMEM;
  617. goto cleanup;
  618. }
  619. rc = initialize_flash_pde_data("ibm,update-flash-64-and-reboot",
  620. sizeof(struct rtas_update_flash_t),
  621. firmware_update_pde);
  622. if (rc != 0)
  623. goto cleanup;
  624. validate_pde = create_flash_pde("powerpc/rtas/" VALIDATE_FLASH_NAME,
  625. &validate_flash_operations);
  626. if (validate_pde == NULL) {
  627. rc = -ENOMEM;
  628. goto cleanup;
  629. }
  630. rc = initialize_flash_pde_data("ibm,validate-flash-image",
  631. sizeof(struct rtas_validate_flash_t),
  632. validate_pde);
  633. if (rc != 0)
  634. goto cleanup;
  635. manage_pde = create_flash_pde("powerpc/rtas/" MANAGE_FLASH_NAME,
  636. &manage_flash_operations);
  637. if (manage_pde == NULL) {
  638. rc = -ENOMEM;
  639. goto cleanup;
  640. }
  641. rc = initialize_flash_pde_data("ibm,manage-flash-image",
  642. sizeof(struct rtas_manage_flash_t),
  643. manage_pde);
  644. if (rc != 0)
  645. goto cleanup;
  646. rtas_flash_term_hook = rtas_flash_firmware;
  647. flash_block_cache = kmem_cache_create("rtas_flash_cache",
  648. RTAS_BLK_SIZE, RTAS_BLK_SIZE, 0,
  649. rtas_block_ctor);
  650. if (!flash_block_cache) {
  651. printk(KERN_ERR "%s: failed to create block cache\n",
  652. __func__);
  653. rc = -ENOMEM;
  654. goto cleanup;
  655. }
  656. return 0;
  657. cleanup:
  658. remove_flash_pde(firmware_flash_pde);
  659. remove_flash_pde(firmware_update_pde);
  660. remove_flash_pde(validate_pde);
  661. remove_flash_pde(manage_pde);
  662. return rc;
  663. }
  664. static void __exit rtas_flash_cleanup(void)
  665. {
  666. rtas_flash_term_hook = NULL;
  667. if (flash_block_cache)
  668. kmem_cache_destroy(flash_block_cache);
  669. remove_flash_pde(firmware_flash_pde);
  670. remove_flash_pde(firmware_update_pde);
  671. remove_flash_pde(validate_pde);
  672. remove_flash_pde(manage_pde);
  673. }
  674. module_init(rtas_flash_init);
  675. module_exit(rtas_flash_cleanup);
  676. MODULE_LICENSE("GPL");