opal-flash.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. /*
  2. * PowerNV OPAL Firmware Update Interface
  3. *
  4. * Copyright 2013 IBM Corp.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #define DEBUG
  12. #include <linux/kernel.h>
  13. #include <linux/reboot.h>
  14. #include <linux/init.h>
  15. #include <linux/kobject.h>
  16. #include <linux/sysfs.h>
  17. #include <linux/slab.h>
  18. #include <linux/mm.h>
  19. #include <linux/vmalloc.h>
  20. #include <linux/pagemap.h>
  21. #include <asm/opal.h>
  22. /* FLASH status codes */
  23. #define FLASH_NO_OP -1099 /* No operation initiated by user */
  24. #define FLASH_NO_AUTH -9002 /* Not a service authority partition */
  25. /* Validate image status values */
  26. #define VALIDATE_IMG_READY -1001 /* Image ready for validation */
  27. #define VALIDATE_IMG_INCOMPLETE -1002 /* User copied < VALIDATE_BUF_SIZE */
  28. /* Manage image status values */
  29. #define MANAGE_ACTIVE_ERR -9001 /* Cannot overwrite active img */
  30. /* Flash image status values */
  31. #define FLASH_IMG_READY 0 /* Img ready for flash on reboot */
  32. #define FLASH_INVALID_IMG -1003 /* Flash image shorter than expected */
  33. #define FLASH_IMG_NULL_DATA -1004 /* Bad data in sg list entry */
  34. #define FLASH_IMG_BAD_LEN -1005 /* Bad length in sg list entry */
  35. /* Manage operation tokens */
  36. #define FLASH_REJECT_TMP_SIDE 0 /* Reject temporary fw image */
  37. #define FLASH_COMMIT_TMP_SIDE 1 /* Commit temporary fw image */
  38. /* Update tokens */
  39. #define FLASH_UPDATE_CANCEL 0 /* Cancel update request */
  40. #define FLASH_UPDATE_INIT 1 /* Initiate update */
  41. /* Validate image update result tokens */
  42. #define VALIDATE_TMP_UPDATE 0 /* T side will be updated */
  43. #define VALIDATE_FLASH_AUTH 1 /* Partition does not have authority */
  44. #define VALIDATE_INVALID_IMG 2 /* Candidate image is not valid */
  45. #define VALIDATE_CUR_UNKNOWN 3 /* Current fixpack level is unknown */
  46. /*
  47. * Current T side will be committed to P side before being replace with new
  48. * image, and the new image is downlevel from current image
  49. */
  50. #define VALIDATE_TMP_COMMIT_DL 4
  51. /*
  52. * Current T side will be committed to P side before being replaced with new
  53. * image
  54. */
  55. #define VALIDATE_TMP_COMMIT 5
  56. /*
  57. * T side will be updated with a downlevel image
  58. */
  59. #define VALIDATE_TMP_UPDATE_DL 6
  60. /*
  61. * The candidate image's release date is later than the system's firmware
  62. * service entitlement date - service warranty period has expired
  63. */
  64. #define VALIDATE_OUT_OF_WRNTY 7
  65. /* Validate buffer size */
  66. #define VALIDATE_BUF_SIZE 4096
  67. /* XXX: Assume candidate image size is <= 256MB */
  68. #define MAX_IMAGE_SIZE 0x10000000
  69. /* Flash sg list version */
  70. #define SG_LIST_VERSION (1UL)
  71. /* Image status */
  72. enum {
  73. IMAGE_INVALID,
  74. IMAGE_LOADING,
  75. IMAGE_READY,
  76. };
  77. /* Candidate image data */
  78. struct image_data_t {
  79. int status;
  80. void *data;
  81. uint32_t size;
  82. };
  83. /* Candidate image header */
  84. struct image_header_t {
  85. uint16_t magic;
  86. uint16_t version;
  87. uint32_t size;
  88. };
  89. /* Scatter/gather entry */
  90. struct opal_sg_entry {
  91. void *data;
  92. long length;
  93. };
  94. /* We calculate number of entries based on PAGE_SIZE */
  95. #define SG_ENTRIES_PER_NODE ((PAGE_SIZE - 16) / sizeof(struct opal_sg_entry))
  96. /*
  97. * This struct is very similar but not identical to that
  98. * needed by the opal flash update. All we need to do for
  99. * opal is rewrite num_entries into a version/length and
  100. * translate the pointers to absolute.
  101. */
  102. struct opal_sg_list {
  103. unsigned long num_entries;
  104. struct opal_sg_list *next;
  105. struct opal_sg_entry entry[SG_ENTRIES_PER_NODE];
  106. };
  107. struct validate_flash_t {
  108. int status; /* Return status */
  109. void *buf; /* Candiate image buffer */
  110. uint32_t buf_size; /* Image size */
  111. uint32_t result; /* Update results token */
  112. };
  113. struct manage_flash_t {
  114. int status; /* Return status */
  115. };
  116. struct update_flash_t {
  117. int status; /* Return status */
  118. };
  119. static struct image_header_t image_header;
  120. static struct image_data_t image_data;
  121. static struct validate_flash_t validate_flash_data;
  122. static struct manage_flash_t manage_flash_data;
  123. static struct update_flash_t update_flash_data;
  124. static DEFINE_MUTEX(image_data_mutex);
  125. /*
  126. * Validate candidate image
  127. */
  128. static inline void opal_flash_validate(void)
  129. {
  130. struct validate_flash_t *args_buf = &validate_flash_data;
  131. args_buf->status = opal_validate_flash(__pa(args_buf->buf),
  132. &(args_buf->buf_size),
  133. &(args_buf->result));
  134. }
  135. /*
  136. * Validate output format:
  137. * validate result token
  138. * current image version details
  139. * new image version details
  140. */
  141. static ssize_t validate_show(struct kobject *kobj,
  142. struct kobj_attribute *attr, char *buf)
  143. {
  144. struct validate_flash_t *args_buf = &validate_flash_data;
  145. int len;
  146. /* Candidate image is not validated */
  147. if (args_buf->status < VALIDATE_TMP_UPDATE) {
  148. len = sprintf(buf, "%d\n", args_buf->status);
  149. goto out;
  150. }
  151. /* Result token */
  152. len = sprintf(buf, "%d\n", args_buf->result);
  153. /* Current and candidate image version details */
  154. if ((args_buf->result != VALIDATE_TMP_UPDATE) &&
  155. (args_buf->result < VALIDATE_CUR_UNKNOWN))
  156. goto out;
  157. if (args_buf->buf_size > (VALIDATE_BUF_SIZE - len)) {
  158. memcpy(buf + len, args_buf->buf, VALIDATE_BUF_SIZE - len);
  159. len = VALIDATE_BUF_SIZE;
  160. } else {
  161. memcpy(buf + len, args_buf->buf, args_buf->buf_size);
  162. len += args_buf->buf_size;
  163. }
  164. out:
  165. /* Set status to default */
  166. args_buf->status = FLASH_NO_OP;
  167. return len;
  168. }
  169. /*
  170. * Validate candidate firmware image
  171. *
  172. * Note:
  173. * We are only interested in first 4K bytes of the
  174. * candidate image.
  175. */
  176. static ssize_t validate_store(struct kobject *kobj,
  177. struct kobj_attribute *attr,
  178. const char *buf, size_t count)
  179. {
  180. struct validate_flash_t *args_buf = &validate_flash_data;
  181. if (buf[0] != '1')
  182. return -EINVAL;
  183. mutex_lock(&image_data_mutex);
  184. if (image_data.status != IMAGE_READY ||
  185. image_data.size < VALIDATE_BUF_SIZE) {
  186. args_buf->result = VALIDATE_INVALID_IMG;
  187. args_buf->status = VALIDATE_IMG_INCOMPLETE;
  188. goto out;
  189. }
  190. /* Copy first 4k bytes of candidate image */
  191. memcpy(args_buf->buf, image_data.data, VALIDATE_BUF_SIZE);
  192. args_buf->status = VALIDATE_IMG_READY;
  193. args_buf->buf_size = VALIDATE_BUF_SIZE;
  194. /* Validate candidate image */
  195. opal_flash_validate();
  196. out:
  197. mutex_unlock(&image_data_mutex);
  198. return count;
  199. }
  200. /*
  201. * Manage flash routine
  202. */
  203. static inline void opal_flash_manage(uint8_t op)
  204. {
  205. struct manage_flash_t *const args_buf = &manage_flash_data;
  206. args_buf->status = opal_manage_flash(op);
  207. }
  208. /*
  209. * Show manage flash status
  210. */
  211. static ssize_t manage_show(struct kobject *kobj,
  212. struct kobj_attribute *attr, char *buf)
  213. {
  214. struct manage_flash_t *const args_buf = &manage_flash_data;
  215. int rc;
  216. rc = sprintf(buf, "%d\n", args_buf->status);
  217. /* Set status to default*/
  218. args_buf->status = FLASH_NO_OP;
  219. return rc;
  220. }
  221. /*
  222. * Manage operations:
  223. * 0 - Reject
  224. * 1 - Commit
  225. */
  226. static ssize_t manage_store(struct kobject *kobj,
  227. struct kobj_attribute *attr,
  228. const char *buf, size_t count)
  229. {
  230. uint8_t op;
  231. switch (buf[0]) {
  232. case '0':
  233. op = FLASH_REJECT_TMP_SIDE;
  234. break;
  235. case '1':
  236. op = FLASH_COMMIT_TMP_SIDE;
  237. break;
  238. default:
  239. return -EINVAL;
  240. }
  241. /* commit/reject temporary image */
  242. opal_flash_manage(op);
  243. return count;
  244. }
  245. /*
  246. * Free sg list
  247. */
  248. static void free_sg_list(struct opal_sg_list *list)
  249. {
  250. struct opal_sg_list *sg1;
  251. while (list) {
  252. sg1 = list->next;
  253. kfree(list);
  254. list = sg1;
  255. }
  256. list = NULL;
  257. }
  258. /*
  259. * Build candidate image scatter gather list
  260. *
  261. * list format:
  262. * -----------------------------------
  263. * | VER (8) | Entry length in bytes |
  264. * -----------------------------------
  265. * | Pointer to next entry |
  266. * -----------------------------------
  267. * | Address of memory area 1 |
  268. * -----------------------------------
  269. * | Length of memory area 1 |
  270. * -----------------------------------
  271. * | ......... |
  272. * -----------------------------------
  273. * | ......... |
  274. * -----------------------------------
  275. * | Address of memory area N |
  276. * -----------------------------------
  277. * | Length of memory area N |
  278. * -----------------------------------
  279. */
  280. static struct opal_sg_list *image_data_to_sglist(void)
  281. {
  282. struct opal_sg_list *sg1, *list = NULL;
  283. void *addr;
  284. int size;
  285. addr = image_data.data;
  286. size = image_data.size;
  287. sg1 = kzalloc((sizeof(struct opal_sg_list)), GFP_KERNEL);
  288. if (!sg1)
  289. return NULL;
  290. list = sg1;
  291. sg1->num_entries = 0;
  292. while (size > 0) {
  293. /* Translate virtual address to physical address */
  294. sg1->entry[sg1->num_entries].data =
  295. (void *)(vmalloc_to_pfn(addr) << PAGE_SHIFT);
  296. if (size > PAGE_SIZE)
  297. sg1->entry[sg1->num_entries].length = PAGE_SIZE;
  298. else
  299. sg1->entry[sg1->num_entries].length = size;
  300. sg1->num_entries++;
  301. if (sg1->num_entries >= SG_ENTRIES_PER_NODE) {
  302. sg1->next = kzalloc((sizeof(struct opal_sg_list)),
  303. GFP_KERNEL);
  304. if (!sg1->next) {
  305. pr_err("%s : Failed to allocate memory\n",
  306. __func__);
  307. goto nomem;
  308. }
  309. sg1 = sg1->next;
  310. sg1->num_entries = 0;
  311. }
  312. addr += PAGE_SIZE;
  313. size -= PAGE_SIZE;
  314. }
  315. return list;
  316. nomem:
  317. free_sg_list(list);
  318. return NULL;
  319. }
  320. /*
  321. * OPAL update flash
  322. */
  323. static int opal_flash_update(int op)
  324. {
  325. struct opal_sg_list *sg, *list, *next;
  326. unsigned long addr;
  327. int64_t rc = OPAL_PARAMETER;
  328. if (op == FLASH_UPDATE_CANCEL) {
  329. pr_alert("FLASH: Image update cancelled\n");
  330. addr = '\0';
  331. goto flash;
  332. }
  333. list = image_data_to_sglist();
  334. if (!list)
  335. goto invalid_img;
  336. /* First entry address */
  337. addr = __pa(list);
  338. /* Translate sg list address to absolute */
  339. for (sg = list; sg; sg = next) {
  340. next = sg->next;
  341. /* Don't translate NULL pointer for last entry */
  342. if (sg->next)
  343. sg->next = (struct opal_sg_list *)__pa(sg->next);
  344. else
  345. sg->next = NULL;
  346. /* Make num_entries into the version/length field */
  347. sg->num_entries = (SG_LIST_VERSION << 56) |
  348. (sg->num_entries * sizeof(struct opal_sg_entry) + 16);
  349. }
  350. pr_alert("FLASH: Image is %u bytes\n", image_data.size);
  351. pr_alert("FLASH: Image update requested\n");
  352. pr_alert("FLASH: Image will be updated during system reboot\n");
  353. pr_alert("FLASH: This will take several minutes. Do not power off!\n");
  354. flash:
  355. rc = opal_update_flash(addr);
  356. invalid_img:
  357. return rc;
  358. }
  359. /*
  360. * Show candidate image status
  361. */
  362. static ssize_t update_show(struct kobject *kobj,
  363. struct kobj_attribute *attr, char *buf)
  364. {
  365. struct update_flash_t *const args_buf = &update_flash_data;
  366. return sprintf(buf, "%d\n", args_buf->status);
  367. }
  368. /*
  369. * Set update image flag
  370. * 1 - Flash new image
  371. * 0 - Cancel flash request
  372. */
  373. static ssize_t update_store(struct kobject *kobj,
  374. struct kobj_attribute *attr,
  375. const char *buf, size_t count)
  376. {
  377. struct update_flash_t *const args_buf = &update_flash_data;
  378. int rc = count;
  379. mutex_lock(&image_data_mutex);
  380. switch (buf[0]) {
  381. case '0':
  382. if (args_buf->status == FLASH_IMG_READY)
  383. opal_flash_update(FLASH_UPDATE_CANCEL);
  384. args_buf->status = FLASH_NO_OP;
  385. break;
  386. case '1':
  387. /* Image is loaded? */
  388. if (image_data.status == IMAGE_READY)
  389. args_buf->status =
  390. opal_flash_update(FLASH_UPDATE_INIT);
  391. else
  392. args_buf->status = FLASH_INVALID_IMG;
  393. break;
  394. default:
  395. rc = -EINVAL;
  396. }
  397. mutex_unlock(&image_data_mutex);
  398. return rc;
  399. }
  400. /*
  401. * Free image buffer
  402. */
  403. static void free_image_buf(void)
  404. {
  405. void *addr;
  406. int size;
  407. addr = image_data.data;
  408. size = PAGE_ALIGN(image_data.size);
  409. while (size > 0) {
  410. ClearPageReserved(vmalloc_to_page(addr));
  411. addr += PAGE_SIZE;
  412. size -= PAGE_SIZE;
  413. }
  414. vfree(image_data.data);
  415. image_data.data = NULL;
  416. image_data.status = IMAGE_INVALID;
  417. }
  418. /*
  419. * Allocate image buffer.
  420. */
  421. static int alloc_image_buf(char *buffer, size_t count)
  422. {
  423. void *addr;
  424. int size;
  425. if (count < sizeof(struct image_header_t)) {
  426. pr_warn("FLASH: Invalid candidate image\n");
  427. return -EINVAL;
  428. }
  429. memcpy(&image_header, (void *)buffer, sizeof(struct image_header_t));
  430. image_data.size = be32_to_cpu(image_header.size);
  431. pr_debug("FLASH: Candiate image size = %u\n", image_data.size);
  432. if (image_data.size > MAX_IMAGE_SIZE) {
  433. pr_warn("FLASH: Too large image\n");
  434. return -EINVAL;
  435. }
  436. if (image_data.size < VALIDATE_BUF_SIZE) {
  437. pr_warn("FLASH: Image is shorter than expected\n");
  438. return -EINVAL;
  439. }
  440. image_data.data = vzalloc(PAGE_ALIGN(image_data.size));
  441. if (!image_data.data) {
  442. pr_err("%s : Failed to allocate memory\n", __func__);
  443. return -ENOMEM;
  444. }
  445. /* Pin memory */
  446. addr = image_data.data;
  447. size = PAGE_ALIGN(image_data.size);
  448. while (size > 0) {
  449. SetPageReserved(vmalloc_to_page(addr));
  450. addr += PAGE_SIZE;
  451. size -= PAGE_SIZE;
  452. }
  453. image_data.status = IMAGE_LOADING;
  454. return 0;
  455. }
  456. /*
  457. * Copy candidate image
  458. *
  459. * Parse candidate image header to get total image size
  460. * and pre-allocate required memory.
  461. */
  462. static ssize_t image_data_write(struct file *filp, struct kobject *kobj,
  463. struct bin_attribute *bin_attr,
  464. char *buffer, loff_t pos, size_t count)
  465. {
  466. int rc;
  467. mutex_lock(&image_data_mutex);
  468. /* New image ? */
  469. if (pos == 0) {
  470. /* Free memory, if already allocated */
  471. if (image_data.data)
  472. free_image_buf();
  473. /* Cancel outstanding image update request */
  474. if (update_flash_data.status == FLASH_IMG_READY)
  475. opal_flash_update(FLASH_UPDATE_CANCEL);
  476. /* Allocate memory */
  477. rc = alloc_image_buf(buffer, count);
  478. if (rc)
  479. goto out;
  480. }
  481. if (image_data.status != IMAGE_LOADING) {
  482. rc = -ENOMEM;
  483. goto out;
  484. }
  485. if ((pos + count) > image_data.size) {
  486. rc = -EINVAL;
  487. goto out;
  488. }
  489. memcpy(image_data.data + pos, (void *)buffer, count);
  490. rc = count;
  491. /* Set image status */
  492. if ((pos + count) == image_data.size) {
  493. pr_debug("FLASH: Candidate image loaded....\n");
  494. image_data.status = IMAGE_READY;
  495. }
  496. out:
  497. mutex_unlock(&image_data_mutex);
  498. return rc;
  499. }
  500. /*
  501. * sysfs interface :
  502. * OPAL uses below sysfs files for code update.
  503. * We create these files under /sys/firmware/opal.
  504. *
  505. * image : Interface to load candidate firmware image
  506. * validate_flash : Validate firmware image
  507. * manage_flash : Commit/Reject firmware image
  508. * update_flash : Flash new firmware image
  509. *
  510. */
  511. static struct bin_attribute image_data_attr = {
  512. .attr = {.name = "image", .mode = 0200},
  513. .size = MAX_IMAGE_SIZE, /* Limit image size */
  514. .write = image_data_write,
  515. };
  516. static struct kobj_attribute validate_attribute =
  517. __ATTR(validate_flash, 0600, validate_show, validate_store);
  518. static struct kobj_attribute manage_attribute =
  519. __ATTR(manage_flash, 0600, manage_show, manage_store);
  520. static struct kobj_attribute update_attribute =
  521. __ATTR(update_flash, 0600, update_show, update_store);
  522. static struct attribute *image_op_attrs[] = {
  523. &validate_attribute.attr,
  524. &manage_attribute.attr,
  525. &update_attribute.attr,
  526. NULL /* need to NULL terminate the list of attributes */
  527. };
  528. static struct attribute_group image_op_attr_group = {
  529. .attrs = image_op_attrs,
  530. };
  531. void __init opal_flash_init(void)
  532. {
  533. int ret;
  534. /* Allocate validate image buffer */
  535. validate_flash_data.buf = kzalloc(VALIDATE_BUF_SIZE, GFP_KERNEL);
  536. if (!validate_flash_data.buf) {
  537. pr_err("%s : Failed to allocate memory\n", __func__);
  538. return;
  539. }
  540. /* Make sure /sys/firmware/opal directory is created */
  541. if (!opal_kobj) {
  542. pr_warn("FLASH: opal kobject is not available\n");
  543. goto nokobj;
  544. }
  545. /* Create the sysfs files */
  546. ret = sysfs_create_group(opal_kobj, &image_op_attr_group);
  547. if (ret) {
  548. pr_warn("FLASH: Failed to create sysfs files\n");
  549. goto nokobj;
  550. }
  551. ret = sysfs_create_bin_file(opal_kobj, &image_data_attr);
  552. if (ret) {
  553. pr_warn("FLASH: Failed to create sysfs files\n");
  554. goto nosysfs_file;
  555. }
  556. /* Set default status */
  557. validate_flash_data.status = FLASH_NO_OP;
  558. manage_flash_data.status = FLASH_NO_OP;
  559. update_flash_data.status = FLASH_NO_OP;
  560. image_data.status = IMAGE_INVALID;
  561. return;
  562. nosysfs_file:
  563. sysfs_remove_group(opal_kobj, &image_op_attr_group);
  564. nokobj:
  565. kfree(validate_flash_data.buf);
  566. return;
  567. }