opal-flash.c 14 KB

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