opal-flash.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  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 <linux/delay.h>
  22. #include <asm/opal.h>
  23. /* FLASH status codes */
  24. #define FLASH_NO_OP -1099 /* No operation initiated by user */
  25. #define FLASH_NO_AUTH -9002 /* Not a service authority partition */
  26. /* Validate image status values */
  27. #define VALIDATE_IMG_READY -1001 /* Image ready for validation */
  28. #define VALIDATE_IMG_INCOMPLETE -1002 /* User copied < VALIDATE_BUF_SIZE */
  29. /* Manage image status values */
  30. #define MANAGE_ACTIVE_ERR -9001 /* Cannot overwrite active img */
  31. /* Flash image status values */
  32. #define FLASH_IMG_READY 0 /* Img ready for flash on reboot */
  33. #define FLASH_INVALID_IMG -1003 /* Flash image shorter than expected */
  34. #define FLASH_IMG_NULL_DATA -1004 /* Bad data in sg list entry */
  35. #define FLASH_IMG_BAD_LEN -1005 /* Bad length in sg list entry */
  36. /* Manage operation tokens */
  37. #define FLASH_REJECT_TMP_SIDE 0 /* Reject temporary fw image */
  38. #define FLASH_COMMIT_TMP_SIDE 1 /* Commit temporary fw image */
  39. /* Update tokens */
  40. #define FLASH_UPDATE_CANCEL 0 /* Cancel update request */
  41. #define FLASH_UPDATE_INIT 1 /* Initiate update */
  42. /* Validate image update result tokens */
  43. #define VALIDATE_TMP_UPDATE 0 /* T side will be updated */
  44. #define VALIDATE_FLASH_AUTH 1 /* Partition does not have authority */
  45. #define VALIDATE_INVALID_IMG 2 /* Candidate image is not valid */
  46. #define VALIDATE_CUR_UNKNOWN 3 /* Current fixpack level is unknown */
  47. /*
  48. * Current T side will be committed to P side before being replace with new
  49. * image, and the new image is downlevel from current image
  50. */
  51. #define VALIDATE_TMP_COMMIT_DL 4
  52. /*
  53. * Current T side will be committed to P side before being replaced with new
  54. * image
  55. */
  56. #define VALIDATE_TMP_COMMIT 5
  57. /*
  58. * T side will be updated with a downlevel image
  59. */
  60. #define VALIDATE_TMP_UPDATE_DL 6
  61. /*
  62. * The candidate image's release date is later than the system's firmware
  63. * service entitlement date - service warranty period has expired
  64. */
  65. #define VALIDATE_OUT_OF_WRNTY 7
  66. /* Validate buffer size */
  67. #define VALIDATE_BUF_SIZE 4096
  68. /* XXX: Assume candidate image size is <= 1GB */
  69. #define MAX_IMAGE_SIZE 0x40000000
  70. /* Image status */
  71. enum {
  72. IMAGE_INVALID,
  73. IMAGE_LOADING,
  74. IMAGE_READY,
  75. };
  76. /* Candidate image data */
  77. struct image_data_t {
  78. int status;
  79. void *data;
  80. uint32_t size;
  81. };
  82. /* Candidate image header */
  83. struct image_header_t {
  84. uint16_t magic;
  85. uint16_t version;
  86. uint32_t size;
  87. };
  88. struct validate_flash_t {
  89. int status; /* Return status */
  90. void *buf; /* Candidate image buffer */
  91. uint32_t buf_size; /* Image size */
  92. uint32_t result; /* Update results token */
  93. };
  94. struct manage_flash_t {
  95. int status; /* Return status */
  96. };
  97. struct update_flash_t {
  98. int status; /* Return status */
  99. };
  100. static struct image_header_t image_header;
  101. static struct image_data_t image_data;
  102. static struct validate_flash_t validate_flash_data;
  103. static struct manage_flash_t manage_flash_data;
  104. /* Initialize update_flash_data status to No Operation */
  105. static struct update_flash_t update_flash_data = {
  106. .status = FLASH_NO_OP,
  107. };
  108. static DEFINE_MUTEX(image_data_mutex);
  109. /*
  110. * Validate candidate image
  111. */
  112. static inline void opal_flash_validate(void)
  113. {
  114. long ret;
  115. void *buf = validate_flash_data.buf;
  116. __be32 size = cpu_to_be32(validate_flash_data.buf_size);
  117. __be32 result;
  118. ret = opal_validate_flash(__pa(buf), &size, &result);
  119. validate_flash_data.status = ret;
  120. validate_flash_data.buf_size = be32_to_cpu(size);
  121. validate_flash_data.result = be32_to_cpu(result);
  122. }
  123. /*
  124. * Validate output format:
  125. * validate result token
  126. * current image version details
  127. * new image version details
  128. */
  129. static ssize_t validate_show(struct kobject *kobj,
  130. struct kobj_attribute *attr, char *buf)
  131. {
  132. struct validate_flash_t *args_buf = &validate_flash_data;
  133. int len;
  134. /* Candidate image is not validated */
  135. if (args_buf->status < VALIDATE_TMP_UPDATE) {
  136. len = sprintf(buf, "%d\n", args_buf->status);
  137. goto out;
  138. }
  139. /* Result token */
  140. len = sprintf(buf, "%d\n", args_buf->result);
  141. /* Current and candidate image version details */
  142. if ((args_buf->result != VALIDATE_TMP_UPDATE) &&
  143. (args_buf->result < VALIDATE_CUR_UNKNOWN))
  144. goto out;
  145. if (args_buf->buf_size > (VALIDATE_BUF_SIZE - len)) {
  146. memcpy(buf + len, args_buf->buf, VALIDATE_BUF_SIZE - len);
  147. len = VALIDATE_BUF_SIZE;
  148. } else {
  149. memcpy(buf + len, args_buf->buf, args_buf->buf_size);
  150. len += args_buf->buf_size;
  151. }
  152. out:
  153. /* Set status to default */
  154. args_buf->status = FLASH_NO_OP;
  155. return len;
  156. }
  157. /*
  158. * Validate candidate firmware image
  159. *
  160. * Note:
  161. * We are only interested in first 4K bytes of the
  162. * candidate image.
  163. */
  164. static ssize_t validate_store(struct kobject *kobj,
  165. struct kobj_attribute *attr,
  166. const char *buf, size_t count)
  167. {
  168. struct validate_flash_t *args_buf = &validate_flash_data;
  169. if (buf[0] != '1')
  170. return -EINVAL;
  171. mutex_lock(&image_data_mutex);
  172. if (image_data.status != IMAGE_READY ||
  173. image_data.size < VALIDATE_BUF_SIZE) {
  174. args_buf->result = VALIDATE_INVALID_IMG;
  175. args_buf->status = VALIDATE_IMG_INCOMPLETE;
  176. goto out;
  177. }
  178. /* Copy first 4k bytes of candidate image */
  179. memcpy(args_buf->buf, image_data.data, VALIDATE_BUF_SIZE);
  180. args_buf->status = VALIDATE_IMG_READY;
  181. args_buf->buf_size = VALIDATE_BUF_SIZE;
  182. /* Validate candidate image */
  183. opal_flash_validate();
  184. out:
  185. mutex_unlock(&image_data_mutex);
  186. return count;
  187. }
  188. /*
  189. * Manage flash routine
  190. */
  191. static inline void opal_flash_manage(uint8_t op)
  192. {
  193. struct manage_flash_t *const args_buf = &manage_flash_data;
  194. args_buf->status = opal_manage_flash(op);
  195. }
  196. /*
  197. * Show manage flash status
  198. */
  199. static ssize_t manage_show(struct kobject *kobj,
  200. struct kobj_attribute *attr, char *buf)
  201. {
  202. struct manage_flash_t *const args_buf = &manage_flash_data;
  203. int rc;
  204. rc = sprintf(buf, "%d\n", args_buf->status);
  205. /* Set status to default*/
  206. args_buf->status = FLASH_NO_OP;
  207. return rc;
  208. }
  209. /*
  210. * Manage operations:
  211. * 0 - Reject
  212. * 1 - Commit
  213. */
  214. static ssize_t manage_store(struct kobject *kobj,
  215. struct kobj_attribute *attr,
  216. const char *buf, size_t count)
  217. {
  218. uint8_t op;
  219. switch (buf[0]) {
  220. case '0':
  221. op = FLASH_REJECT_TMP_SIDE;
  222. break;
  223. case '1':
  224. op = FLASH_COMMIT_TMP_SIDE;
  225. break;
  226. default:
  227. return -EINVAL;
  228. }
  229. /* commit/reject temporary image */
  230. opal_flash_manage(op);
  231. return count;
  232. }
  233. /*
  234. * OPAL update flash
  235. */
  236. static int opal_flash_update(int op)
  237. {
  238. struct opal_sg_list *list;
  239. unsigned long addr;
  240. int64_t rc = OPAL_PARAMETER;
  241. if (op == FLASH_UPDATE_CANCEL) {
  242. pr_alert("FLASH: Image update cancelled\n");
  243. addr = '\0';
  244. goto flash;
  245. }
  246. list = opal_vmalloc_to_sg_list(image_data.data, image_data.size);
  247. if (!list)
  248. goto invalid_img;
  249. /* First entry address */
  250. addr = __pa(list);
  251. flash:
  252. rc = opal_update_flash(addr);
  253. invalid_img:
  254. return rc;
  255. }
  256. /* Return CPUs to OPAL before starting FW update */
  257. static void flash_return_cpu(void *info)
  258. {
  259. int cpu = smp_processor_id();
  260. if (!cpu_online(cpu))
  261. return;
  262. /* Disable IRQ */
  263. hard_irq_disable();
  264. /* Return the CPU to OPAL */
  265. opal_return_cpu();
  266. }
  267. /* This gets called just before system reboots */
  268. void opal_flash_term_callback(void)
  269. {
  270. struct cpumask mask;
  271. if (update_flash_data.status != FLASH_IMG_READY)
  272. return;
  273. pr_alert("FLASH: Flashing new firmware\n");
  274. pr_alert("FLASH: Image is %u bytes\n", image_data.size);
  275. pr_alert("FLASH: Performing flash and reboot/shutdown\n");
  276. pr_alert("FLASH: This will take several minutes. Do not power off!\n");
  277. /* Small delay to help getting the above message out */
  278. msleep(500);
  279. /* Return secondary CPUs to firmware */
  280. cpumask_copy(&mask, cpu_online_mask);
  281. cpumask_clear_cpu(smp_processor_id(), &mask);
  282. if (!cpumask_empty(&mask))
  283. smp_call_function_many(&mask,
  284. flash_return_cpu, NULL, false);
  285. /* Hard disable interrupts */
  286. hard_irq_disable();
  287. }
  288. /*
  289. * Show candidate image status
  290. */
  291. static ssize_t update_show(struct kobject *kobj,
  292. struct kobj_attribute *attr, char *buf)
  293. {
  294. struct update_flash_t *const args_buf = &update_flash_data;
  295. return sprintf(buf, "%d\n", args_buf->status);
  296. }
  297. /*
  298. * Set update image flag
  299. * 1 - Flash new image
  300. * 0 - Cancel flash request
  301. */
  302. static ssize_t update_store(struct kobject *kobj,
  303. struct kobj_attribute *attr,
  304. const char *buf, size_t count)
  305. {
  306. struct update_flash_t *const args_buf = &update_flash_data;
  307. int rc = count;
  308. mutex_lock(&image_data_mutex);
  309. switch (buf[0]) {
  310. case '0':
  311. if (args_buf->status == FLASH_IMG_READY)
  312. opal_flash_update(FLASH_UPDATE_CANCEL);
  313. args_buf->status = FLASH_NO_OP;
  314. break;
  315. case '1':
  316. /* Image is loaded? */
  317. if (image_data.status == IMAGE_READY)
  318. args_buf->status =
  319. opal_flash_update(FLASH_UPDATE_INIT);
  320. else
  321. args_buf->status = FLASH_INVALID_IMG;
  322. break;
  323. default:
  324. rc = -EINVAL;
  325. }
  326. mutex_unlock(&image_data_mutex);
  327. return rc;
  328. }
  329. /*
  330. * Free image buffer
  331. */
  332. static void free_image_buf(void)
  333. {
  334. void *addr;
  335. int size;
  336. addr = image_data.data;
  337. size = PAGE_ALIGN(image_data.size);
  338. while (size > 0) {
  339. ClearPageReserved(vmalloc_to_page(addr));
  340. addr += PAGE_SIZE;
  341. size -= PAGE_SIZE;
  342. }
  343. vfree(image_data.data);
  344. image_data.data = NULL;
  345. image_data.status = IMAGE_INVALID;
  346. }
  347. /*
  348. * Allocate image buffer.
  349. */
  350. static int alloc_image_buf(char *buffer, size_t count)
  351. {
  352. void *addr;
  353. int size;
  354. if (count < sizeof(struct image_header_t)) {
  355. pr_warn("FLASH: Invalid candidate image\n");
  356. return -EINVAL;
  357. }
  358. memcpy(&image_header, (void *)buffer, sizeof(struct image_header_t));
  359. image_data.size = be32_to_cpu(image_header.size);
  360. pr_debug("FLASH: Candidate image size = %u\n", image_data.size);
  361. if (image_data.size > MAX_IMAGE_SIZE) {
  362. pr_warn("FLASH: Too large image\n");
  363. return -EINVAL;
  364. }
  365. if (image_data.size < VALIDATE_BUF_SIZE) {
  366. pr_warn("FLASH: Image is shorter than expected\n");
  367. return -EINVAL;
  368. }
  369. image_data.data = vzalloc(PAGE_ALIGN(image_data.size));
  370. if (!image_data.data) {
  371. pr_err("%s : Failed to allocate memory\n", __func__);
  372. return -ENOMEM;
  373. }
  374. /* Pin memory */
  375. addr = image_data.data;
  376. size = PAGE_ALIGN(image_data.size);
  377. while (size > 0) {
  378. SetPageReserved(vmalloc_to_page(addr));
  379. addr += PAGE_SIZE;
  380. size -= PAGE_SIZE;
  381. }
  382. image_data.status = IMAGE_LOADING;
  383. return 0;
  384. }
  385. /*
  386. * Copy candidate image
  387. *
  388. * Parse candidate image header to get total image size
  389. * and pre-allocate required memory.
  390. */
  391. static ssize_t image_data_write(struct file *filp, struct kobject *kobj,
  392. struct bin_attribute *bin_attr,
  393. char *buffer, loff_t pos, size_t count)
  394. {
  395. int rc;
  396. mutex_lock(&image_data_mutex);
  397. /* New image ? */
  398. if (pos == 0) {
  399. /* Free memory, if already allocated */
  400. if (image_data.data)
  401. free_image_buf();
  402. /* Cancel outstanding image update request */
  403. if (update_flash_data.status == FLASH_IMG_READY)
  404. opal_flash_update(FLASH_UPDATE_CANCEL);
  405. /* Allocate memory */
  406. rc = alloc_image_buf(buffer, count);
  407. if (rc)
  408. goto out;
  409. }
  410. if (image_data.status != IMAGE_LOADING) {
  411. rc = -ENOMEM;
  412. goto out;
  413. }
  414. if ((pos + count) > image_data.size) {
  415. rc = -EINVAL;
  416. goto out;
  417. }
  418. memcpy(image_data.data + pos, (void *)buffer, count);
  419. rc = count;
  420. /* Set image status */
  421. if ((pos + count) == image_data.size) {
  422. pr_debug("FLASH: Candidate image loaded....\n");
  423. image_data.status = IMAGE_READY;
  424. }
  425. out:
  426. mutex_unlock(&image_data_mutex);
  427. return rc;
  428. }
  429. /*
  430. * sysfs interface :
  431. * OPAL uses below sysfs files for code update.
  432. * We create these files under /sys/firmware/opal.
  433. *
  434. * image : Interface to load candidate firmware image
  435. * validate_flash : Validate firmware image
  436. * manage_flash : Commit/Reject firmware image
  437. * update_flash : Flash new firmware image
  438. *
  439. */
  440. static struct bin_attribute image_data_attr = {
  441. .attr = {.name = "image", .mode = 0200},
  442. .size = MAX_IMAGE_SIZE, /* Limit image size */
  443. .write = image_data_write,
  444. };
  445. static struct kobj_attribute validate_attribute =
  446. __ATTR(validate_flash, 0600, validate_show, validate_store);
  447. static struct kobj_attribute manage_attribute =
  448. __ATTR(manage_flash, 0600, manage_show, manage_store);
  449. static struct kobj_attribute update_attribute =
  450. __ATTR(update_flash, 0600, update_show, update_store);
  451. static struct attribute *image_op_attrs[] = {
  452. &validate_attribute.attr,
  453. &manage_attribute.attr,
  454. &update_attribute.attr,
  455. NULL /* need to NULL terminate the list of attributes */
  456. };
  457. static struct attribute_group image_op_attr_group = {
  458. .attrs = image_op_attrs,
  459. };
  460. void __init opal_flash_update_init(void)
  461. {
  462. int ret;
  463. /* Allocate validate image buffer */
  464. validate_flash_data.buf = kzalloc(VALIDATE_BUF_SIZE, GFP_KERNEL);
  465. if (!validate_flash_data.buf) {
  466. pr_err("%s : Failed to allocate memory\n", __func__);
  467. return;
  468. }
  469. /* Make sure /sys/firmware/opal directory is created */
  470. if (!opal_kobj) {
  471. pr_warn("FLASH: opal kobject is not available\n");
  472. goto nokobj;
  473. }
  474. /* Create the sysfs files */
  475. ret = sysfs_create_group(opal_kobj, &image_op_attr_group);
  476. if (ret) {
  477. pr_warn("FLASH: Failed to create sysfs files\n");
  478. goto nokobj;
  479. }
  480. ret = sysfs_create_bin_file(opal_kobj, &image_data_attr);
  481. if (ret) {
  482. pr_warn("FLASH: Failed to create sysfs files\n");
  483. goto nosysfs_file;
  484. }
  485. /* Set default status */
  486. validate_flash_data.status = FLASH_NO_OP;
  487. manage_flash_data.status = FLASH_NO_OP;
  488. update_flash_data.status = FLASH_NO_OP;
  489. image_data.status = IMAGE_INVALID;
  490. return;
  491. nosysfs_file:
  492. sysfs_remove_group(opal_kobj, &image_op_attr_group);
  493. nokobj:
  494. kfree(validate_flash_data.buf);
  495. return;
  496. }