opal-flash.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  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 <= 1GB */
  68. #define MAX_IMAGE_SIZE 0x40000000
  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. struct validate_flash_t {
  90. int status; /* Return status */
  91. void *buf; /* Candidate image buffer */
  92. uint32_t buf_size; /* Image size */
  93. uint32_t result; /* Update results token */
  94. };
  95. struct manage_flash_t {
  96. int status; /* Return status */
  97. };
  98. struct update_flash_t {
  99. int status; /* Return status */
  100. };
  101. static struct image_header_t image_header;
  102. static struct image_data_t image_data;
  103. static struct validate_flash_t validate_flash_data;
  104. static struct manage_flash_t manage_flash_data;
  105. static struct update_flash_t update_flash_data;
  106. static DEFINE_MUTEX(image_data_mutex);
  107. /*
  108. * Validate candidate image
  109. */
  110. static inline void opal_flash_validate(void)
  111. {
  112. struct validate_flash_t *args_buf = &validate_flash_data;
  113. args_buf->status = opal_validate_flash(__pa(args_buf->buf),
  114. &(args_buf->buf_size),
  115. &(args_buf->result));
  116. }
  117. /*
  118. * Validate output format:
  119. * validate result token
  120. * current image version details
  121. * new image version details
  122. */
  123. static ssize_t validate_show(struct kobject *kobj,
  124. struct kobj_attribute *attr, char *buf)
  125. {
  126. struct validate_flash_t *args_buf = &validate_flash_data;
  127. int len;
  128. /* Candidate image is not validated */
  129. if (args_buf->status < VALIDATE_TMP_UPDATE) {
  130. len = sprintf(buf, "%d\n", args_buf->status);
  131. goto out;
  132. }
  133. /* Result token */
  134. len = sprintf(buf, "%d\n", args_buf->result);
  135. /* Current and candidate image version details */
  136. if ((args_buf->result != VALIDATE_TMP_UPDATE) &&
  137. (args_buf->result < VALIDATE_CUR_UNKNOWN))
  138. goto out;
  139. if (args_buf->buf_size > (VALIDATE_BUF_SIZE - len)) {
  140. memcpy(buf + len, args_buf->buf, VALIDATE_BUF_SIZE - len);
  141. len = VALIDATE_BUF_SIZE;
  142. } else {
  143. memcpy(buf + len, args_buf->buf, args_buf->buf_size);
  144. len += args_buf->buf_size;
  145. }
  146. out:
  147. /* Set status to default */
  148. args_buf->status = FLASH_NO_OP;
  149. return len;
  150. }
  151. /*
  152. * Validate candidate firmware image
  153. *
  154. * Note:
  155. * We are only interested in first 4K bytes of the
  156. * candidate image.
  157. */
  158. static ssize_t validate_store(struct kobject *kobj,
  159. struct kobj_attribute *attr,
  160. const char *buf, size_t count)
  161. {
  162. struct validate_flash_t *args_buf = &validate_flash_data;
  163. if (buf[0] != '1')
  164. return -EINVAL;
  165. mutex_lock(&image_data_mutex);
  166. if (image_data.status != IMAGE_READY ||
  167. image_data.size < VALIDATE_BUF_SIZE) {
  168. args_buf->result = VALIDATE_INVALID_IMG;
  169. args_buf->status = VALIDATE_IMG_INCOMPLETE;
  170. goto out;
  171. }
  172. /* Copy first 4k bytes of candidate image */
  173. memcpy(args_buf->buf, image_data.data, VALIDATE_BUF_SIZE);
  174. args_buf->status = VALIDATE_IMG_READY;
  175. args_buf->buf_size = VALIDATE_BUF_SIZE;
  176. /* Validate candidate image */
  177. opal_flash_validate();
  178. out:
  179. mutex_unlock(&image_data_mutex);
  180. return count;
  181. }
  182. /*
  183. * Manage flash routine
  184. */
  185. static inline void opal_flash_manage(uint8_t op)
  186. {
  187. struct manage_flash_t *const args_buf = &manage_flash_data;
  188. args_buf->status = opal_manage_flash(op);
  189. }
  190. /*
  191. * Show manage flash status
  192. */
  193. static ssize_t manage_show(struct kobject *kobj,
  194. struct kobj_attribute *attr, char *buf)
  195. {
  196. struct manage_flash_t *const args_buf = &manage_flash_data;
  197. int rc;
  198. rc = sprintf(buf, "%d\n", args_buf->status);
  199. /* Set status to default*/
  200. args_buf->status = FLASH_NO_OP;
  201. return rc;
  202. }
  203. /*
  204. * Manage operations:
  205. * 0 - Reject
  206. * 1 - Commit
  207. */
  208. static ssize_t manage_store(struct kobject *kobj,
  209. struct kobj_attribute *attr,
  210. const char *buf, size_t count)
  211. {
  212. uint8_t op;
  213. switch (buf[0]) {
  214. case '0':
  215. op = FLASH_REJECT_TMP_SIDE;
  216. break;
  217. case '1':
  218. op = FLASH_COMMIT_TMP_SIDE;
  219. break;
  220. default:
  221. return -EINVAL;
  222. }
  223. /* commit/reject temporary image */
  224. opal_flash_manage(op);
  225. return count;
  226. }
  227. /*
  228. * Free sg list
  229. */
  230. static void free_sg_list(struct opal_sg_list *list)
  231. {
  232. struct opal_sg_list *sg1;
  233. while (list) {
  234. sg1 = list->next;
  235. kfree(list);
  236. list = sg1;
  237. }
  238. list = NULL;
  239. }
  240. /*
  241. * Build candidate image scatter gather list
  242. *
  243. * list format:
  244. * -----------------------------------
  245. * | VER (8) | Entry length in bytes |
  246. * -----------------------------------
  247. * | Pointer to next entry |
  248. * -----------------------------------
  249. * | Address of memory area 1 |
  250. * -----------------------------------
  251. * | Length of memory area 1 |
  252. * -----------------------------------
  253. * | ......... |
  254. * -----------------------------------
  255. * | ......... |
  256. * -----------------------------------
  257. * | Address of memory area N |
  258. * -----------------------------------
  259. * | Length of memory area N |
  260. * -----------------------------------
  261. */
  262. static struct opal_sg_list *image_data_to_sglist(void)
  263. {
  264. struct opal_sg_list *sg1, *list = NULL;
  265. void *addr;
  266. int size;
  267. addr = image_data.data;
  268. size = image_data.size;
  269. sg1 = kzalloc(PAGE_SIZE, GFP_KERNEL);
  270. if (!sg1)
  271. return NULL;
  272. list = sg1;
  273. sg1->num_entries = 0;
  274. while (size > 0) {
  275. /* Translate virtual address to physical address */
  276. sg1->entry[sg1->num_entries].data =
  277. (void *)(vmalloc_to_pfn(addr) << PAGE_SHIFT);
  278. if (size > PAGE_SIZE)
  279. sg1->entry[sg1->num_entries].length = PAGE_SIZE;
  280. else
  281. sg1->entry[sg1->num_entries].length = size;
  282. sg1->num_entries++;
  283. if (sg1->num_entries >= SG_ENTRIES_PER_NODE) {
  284. sg1->next = kzalloc(PAGE_SIZE, GFP_KERNEL);
  285. if (!sg1->next) {
  286. pr_err("%s : Failed to allocate memory\n",
  287. __func__);
  288. goto nomem;
  289. }
  290. sg1 = sg1->next;
  291. sg1->num_entries = 0;
  292. }
  293. addr += PAGE_SIZE;
  294. size -= PAGE_SIZE;
  295. }
  296. return list;
  297. nomem:
  298. free_sg_list(list);
  299. return NULL;
  300. }
  301. /*
  302. * OPAL update flash
  303. */
  304. static int opal_flash_update(int op)
  305. {
  306. struct opal_sg_list *sg, *list, *next;
  307. unsigned long addr;
  308. int64_t rc = OPAL_PARAMETER;
  309. if (op == FLASH_UPDATE_CANCEL) {
  310. pr_alert("FLASH: Image update cancelled\n");
  311. addr = '\0';
  312. goto flash;
  313. }
  314. list = image_data_to_sglist();
  315. if (!list)
  316. goto invalid_img;
  317. /* First entry address */
  318. addr = __pa(list);
  319. /* Translate sg list address to absolute */
  320. for (sg = list; sg; sg = next) {
  321. next = sg->next;
  322. /* Don't translate NULL pointer for last entry */
  323. if (sg->next)
  324. sg->next = (struct opal_sg_list *)__pa(sg->next);
  325. else
  326. sg->next = NULL;
  327. /*
  328. * Convert num_entries to version/length format
  329. * to satisfy OPAL.
  330. */
  331. sg->num_entries = (SG_LIST_VERSION << 56) |
  332. (sg->num_entries * sizeof(struct opal_sg_entry) + 16);
  333. }
  334. pr_alert("FLASH: Image is %u bytes\n", image_data.size);
  335. pr_alert("FLASH: Image update requested\n");
  336. pr_alert("FLASH: Image will be updated during system reboot\n");
  337. pr_alert("FLASH: This will take several minutes. Do not power off!\n");
  338. flash:
  339. rc = opal_update_flash(addr);
  340. invalid_img:
  341. return rc;
  342. }
  343. /*
  344. * Show candidate image status
  345. */
  346. static ssize_t update_show(struct kobject *kobj,
  347. struct kobj_attribute *attr, char *buf)
  348. {
  349. struct update_flash_t *const args_buf = &update_flash_data;
  350. return sprintf(buf, "%d\n", args_buf->status);
  351. }
  352. /*
  353. * Set update image flag
  354. * 1 - Flash new image
  355. * 0 - Cancel flash request
  356. */
  357. static ssize_t update_store(struct kobject *kobj,
  358. struct kobj_attribute *attr,
  359. const char *buf, size_t count)
  360. {
  361. struct update_flash_t *const args_buf = &update_flash_data;
  362. int rc = count;
  363. mutex_lock(&image_data_mutex);
  364. switch (buf[0]) {
  365. case '0':
  366. if (args_buf->status == FLASH_IMG_READY)
  367. opal_flash_update(FLASH_UPDATE_CANCEL);
  368. args_buf->status = FLASH_NO_OP;
  369. break;
  370. case '1':
  371. /* Image is loaded? */
  372. if (image_data.status == IMAGE_READY)
  373. args_buf->status =
  374. opal_flash_update(FLASH_UPDATE_INIT);
  375. else
  376. args_buf->status = FLASH_INVALID_IMG;
  377. break;
  378. default:
  379. rc = -EINVAL;
  380. }
  381. mutex_unlock(&image_data_mutex);
  382. return rc;
  383. }
  384. /*
  385. * Free image buffer
  386. */
  387. static void free_image_buf(void)
  388. {
  389. void *addr;
  390. int size;
  391. addr = image_data.data;
  392. size = PAGE_ALIGN(image_data.size);
  393. while (size > 0) {
  394. ClearPageReserved(vmalloc_to_page(addr));
  395. addr += PAGE_SIZE;
  396. size -= PAGE_SIZE;
  397. }
  398. vfree(image_data.data);
  399. image_data.data = NULL;
  400. image_data.status = IMAGE_INVALID;
  401. }
  402. /*
  403. * Allocate image buffer.
  404. */
  405. static int alloc_image_buf(char *buffer, size_t count)
  406. {
  407. void *addr;
  408. int size;
  409. if (count < sizeof(struct image_header_t)) {
  410. pr_warn("FLASH: Invalid candidate image\n");
  411. return -EINVAL;
  412. }
  413. memcpy(&image_header, (void *)buffer, sizeof(struct image_header_t));
  414. image_data.size = be32_to_cpu(image_header.size);
  415. pr_debug("FLASH: Candidate image size = %u\n", image_data.size);
  416. if (image_data.size > MAX_IMAGE_SIZE) {
  417. pr_warn("FLASH: Too large image\n");
  418. return -EINVAL;
  419. }
  420. if (image_data.size < VALIDATE_BUF_SIZE) {
  421. pr_warn("FLASH: Image is shorter than expected\n");
  422. return -EINVAL;
  423. }
  424. image_data.data = vzalloc(PAGE_ALIGN(image_data.size));
  425. if (!image_data.data) {
  426. pr_err("%s : Failed to allocate memory\n", __func__);
  427. return -ENOMEM;
  428. }
  429. /* Pin memory */
  430. addr = image_data.data;
  431. size = PAGE_ALIGN(image_data.size);
  432. while (size > 0) {
  433. SetPageReserved(vmalloc_to_page(addr));
  434. addr += PAGE_SIZE;
  435. size -= PAGE_SIZE;
  436. }
  437. image_data.status = IMAGE_LOADING;
  438. return 0;
  439. }
  440. /*
  441. * Copy candidate image
  442. *
  443. * Parse candidate image header to get total image size
  444. * and pre-allocate required memory.
  445. */
  446. static ssize_t image_data_write(struct file *filp, struct kobject *kobj,
  447. struct bin_attribute *bin_attr,
  448. char *buffer, loff_t pos, size_t count)
  449. {
  450. int rc;
  451. mutex_lock(&image_data_mutex);
  452. /* New image ? */
  453. if (pos == 0) {
  454. /* Free memory, if already allocated */
  455. if (image_data.data)
  456. free_image_buf();
  457. /* Cancel outstanding image update request */
  458. if (update_flash_data.status == FLASH_IMG_READY)
  459. opal_flash_update(FLASH_UPDATE_CANCEL);
  460. /* Allocate memory */
  461. rc = alloc_image_buf(buffer, count);
  462. if (rc)
  463. goto out;
  464. }
  465. if (image_data.status != IMAGE_LOADING) {
  466. rc = -ENOMEM;
  467. goto out;
  468. }
  469. if ((pos + count) > image_data.size) {
  470. rc = -EINVAL;
  471. goto out;
  472. }
  473. memcpy(image_data.data + pos, (void *)buffer, count);
  474. rc = count;
  475. /* Set image status */
  476. if ((pos + count) == image_data.size) {
  477. pr_debug("FLASH: Candidate image loaded....\n");
  478. image_data.status = IMAGE_READY;
  479. }
  480. out:
  481. mutex_unlock(&image_data_mutex);
  482. return rc;
  483. }
  484. /*
  485. * sysfs interface :
  486. * OPAL uses below sysfs files for code update.
  487. * We create these files under /sys/firmware/opal.
  488. *
  489. * image : Interface to load candidate firmware image
  490. * validate_flash : Validate firmware image
  491. * manage_flash : Commit/Reject firmware image
  492. * update_flash : Flash new firmware image
  493. *
  494. */
  495. static struct bin_attribute image_data_attr = {
  496. .attr = {.name = "image", .mode = 0200},
  497. .size = MAX_IMAGE_SIZE, /* Limit image size */
  498. .write = image_data_write,
  499. };
  500. static struct kobj_attribute validate_attribute =
  501. __ATTR(validate_flash, 0600, validate_show, validate_store);
  502. static struct kobj_attribute manage_attribute =
  503. __ATTR(manage_flash, 0600, manage_show, manage_store);
  504. static struct kobj_attribute update_attribute =
  505. __ATTR(update_flash, 0600, update_show, update_store);
  506. static struct attribute *image_op_attrs[] = {
  507. &validate_attribute.attr,
  508. &manage_attribute.attr,
  509. &update_attribute.attr,
  510. NULL /* need to NULL terminate the list of attributes */
  511. };
  512. static struct attribute_group image_op_attr_group = {
  513. .attrs = image_op_attrs,
  514. };
  515. void __init opal_flash_init(void)
  516. {
  517. int ret;
  518. /* Allocate validate image buffer */
  519. validate_flash_data.buf = kzalloc(VALIDATE_BUF_SIZE, GFP_KERNEL);
  520. if (!validate_flash_data.buf) {
  521. pr_err("%s : Failed to allocate memory\n", __func__);
  522. return;
  523. }
  524. /* Make sure /sys/firmware/opal directory is created */
  525. if (!opal_kobj) {
  526. pr_warn("FLASH: opal kobject is not available\n");
  527. goto nokobj;
  528. }
  529. /* Create the sysfs files */
  530. ret = sysfs_create_group(opal_kobj, &image_op_attr_group);
  531. if (ret) {
  532. pr_warn("FLASH: Failed to create sysfs files\n");
  533. goto nokobj;
  534. }
  535. ret = sysfs_create_bin_file(opal_kobj, &image_data_attr);
  536. if (ret) {
  537. pr_warn("FLASH: Failed to create sysfs files\n");
  538. goto nosysfs_file;
  539. }
  540. /* Set default status */
  541. validate_flash_data.status = FLASH_NO_OP;
  542. manage_flash_data.status = FLASH_NO_OP;
  543. update_flash_data.status = FLASH_NO_OP;
  544. image_data.status = IMAGE_INVALID;
  545. return;
  546. nosysfs_file:
  547. sysfs_remove_group(opal_kobj, &image_op_attr_group);
  548. nokobj:
  549. kfree(validate_flash_data.buf);
  550. return;
  551. }