2 * PowerNV OPAL Firmware Update Interface
4 * Copyright 2013 IBM Corp.
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.
14 #include <linux/kernel.h>
15 #include <linux/reboot.h>
16 #include <linux/init.h>
17 #include <linux/kobject.h>
18 #include <linux/sysfs.h>
19 #include <linux/slab.h>
21 #include <linux/vmalloc.h>
22 #include <linux/pagemap.h>
23 #include <linux/delay.h>
27 /* FLASH status codes */
28 #define FLASH_NO_OP -1099 /* No operation initiated by user */
29 #define FLASH_NO_AUTH -9002 /* Not a service authority partition */
31 /* Validate image status values */
32 #define VALIDATE_IMG_READY -1001 /* Image ready for validation */
33 #define VALIDATE_IMG_INCOMPLETE -1002 /* User copied < VALIDATE_BUF_SIZE */
35 /* Manage image status values */
36 #define MANAGE_ACTIVE_ERR -9001 /* Cannot overwrite active img */
38 /* Flash image status values */
39 #define FLASH_IMG_READY 0 /* Img ready for flash on reboot */
40 #define FLASH_INVALID_IMG -1003 /* Flash image shorter than expected */
41 #define FLASH_IMG_NULL_DATA -1004 /* Bad data in sg list entry */
42 #define FLASH_IMG_BAD_LEN -1005 /* Bad length in sg list entry */
44 /* Manage operation tokens */
45 #define FLASH_REJECT_TMP_SIDE 0 /* Reject temporary fw image */
46 #define FLASH_COMMIT_TMP_SIDE 1 /* Commit temporary fw image */
49 #define FLASH_UPDATE_CANCEL 0 /* Cancel update request */
50 #define FLASH_UPDATE_INIT 1 /* Initiate update */
52 /* Validate image update result tokens */
53 #define VALIDATE_TMP_UPDATE 0 /* T side will be updated */
54 #define VALIDATE_FLASH_AUTH 1 /* Partition does not have authority */
55 #define VALIDATE_INVALID_IMG 2 /* Candidate image is not valid */
56 #define VALIDATE_CUR_UNKNOWN 3 /* Current fixpack level is unknown */
58 * Current T side will be committed to P side before being replace with new
59 * image, and the new image is downlevel from current image
61 #define VALIDATE_TMP_COMMIT_DL 4
63 * Current T side will be committed to P side before being replaced with new
66 #define VALIDATE_TMP_COMMIT 5
68 * T side will be updated with a downlevel image
70 #define VALIDATE_TMP_UPDATE_DL 6
72 * The candidate image's release date is later than the system's firmware
73 * service entitlement date - service warranty period has expired
75 #define VALIDATE_OUT_OF_WRNTY 7
77 /* Validate buffer size */
78 #define VALIDATE_BUF_SIZE 4096
80 /* XXX: Assume candidate image size is <= 1GB */
81 #define MAX_IMAGE_SIZE 0x40000000
90 /* Candidate image data */
97 /* Candidate image header */
98 struct image_header_t {
104 struct validate_flash_t {
105 int status; /* Return status */
106 void *buf; /* Candidate image buffer */
107 uint32_t buf_size; /* Image size */
108 uint32_t result; /* Update results token */
111 struct manage_flash_t {
112 int status; /* Return status */
115 struct update_flash_t {
116 int status; /* Return status */
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;
125 static DEFINE_MUTEX(image_data_mutex);
128 * Validate candidate image
130 static inline void opal_flash_validate(void)
133 void *buf = validate_flash_data.buf;
134 __be32 size = cpu_to_be32(validate_flash_data.buf_size);
137 ret = opal_validate_flash(__pa(buf), &size, &result);
139 validate_flash_data.status = ret;
140 validate_flash_data.buf_size = be32_to_cpu(size);
141 validate_flash_data.result = be32_to_cpu(result);
145 * Validate output format:
146 * validate result token
147 * current image version details
148 * new image version details
150 static ssize_t validate_show(struct kobject *kobj,
151 struct kobj_attribute *attr, char *buf)
153 struct validate_flash_t *args_buf = &validate_flash_data;
156 /* Candidate image is not validated */
157 if (args_buf->status < VALIDATE_TMP_UPDATE) {
158 len = sprintf(buf, "%d\n", args_buf->status);
163 len = sprintf(buf, "%d\n", args_buf->result);
165 /* Current and candidate image version details */
166 if ((args_buf->result != VALIDATE_TMP_UPDATE) &&
167 (args_buf->result < VALIDATE_CUR_UNKNOWN))
170 if (args_buf->buf_size > (VALIDATE_BUF_SIZE - len)) {
171 memcpy(buf + len, args_buf->buf, VALIDATE_BUF_SIZE - len);
172 len = VALIDATE_BUF_SIZE;
174 memcpy(buf + len, args_buf->buf, args_buf->buf_size);
175 len += args_buf->buf_size;
178 /* Set status to default */
179 args_buf->status = FLASH_NO_OP;
184 * Validate candidate firmware image
187 * We are only interested in first 4K bytes of the
190 static ssize_t validate_store(struct kobject *kobj,
191 struct kobj_attribute *attr,
192 const char *buf, size_t count)
194 struct validate_flash_t *args_buf = &validate_flash_data;
199 mutex_lock(&image_data_mutex);
201 if (image_data.status != IMAGE_READY ||
202 image_data.size < VALIDATE_BUF_SIZE) {
203 args_buf->result = VALIDATE_INVALID_IMG;
204 args_buf->status = VALIDATE_IMG_INCOMPLETE;
208 /* Copy first 4k bytes of candidate image */
209 memcpy(args_buf->buf, image_data.data, VALIDATE_BUF_SIZE);
211 args_buf->status = VALIDATE_IMG_READY;
212 args_buf->buf_size = VALIDATE_BUF_SIZE;
214 /* Validate candidate image */
215 opal_flash_validate();
218 mutex_unlock(&image_data_mutex);
223 * Manage flash routine
225 static inline void opal_flash_manage(uint8_t op)
227 struct manage_flash_t *const args_buf = &manage_flash_data;
229 args_buf->status = opal_manage_flash(op);
233 * Show manage flash status
235 static ssize_t manage_show(struct kobject *kobj,
236 struct kobj_attribute *attr, char *buf)
238 struct manage_flash_t *const args_buf = &manage_flash_data;
241 rc = sprintf(buf, "%d\n", args_buf->status);
242 /* Set status to default*/
243 args_buf->status = FLASH_NO_OP;
252 static ssize_t manage_store(struct kobject *kobj,
253 struct kobj_attribute *attr,
254 const char *buf, size_t count)
259 op = FLASH_REJECT_TMP_SIDE;
262 op = FLASH_COMMIT_TMP_SIDE;
268 /* commit/reject temporary image */
269 opal_flash_manage(op);
276 static int opal_flash_update(int op)
278 struct opal_sg_list *list;
280 int64_t rc = OPAL_PARAMETER;
282 if (op == FLASH_UPDATE_CANCEL) {
283 pr_alert("FLASH: Image update cancelled\n");
288 list = opal_vmalloc_to_sg_list(image_data.data, image_data.size);
292 /* First entry address */
296 rc = opal_update_flash(addr);
302 /* Return CPUs to OPAL before starting FW update */
303 static void flash_return_cpu(void *info)
305 int cpu = smp_processor_id();
307 if (!cpu_online(cpu))
313 /* Return the CPU to OPAL */
317 /* This gets called just before system reboots */
318 void opal_flash_term_callback(void)
322 if (update_flash_data.status != FLASH_IMG_READY)
325 pr_alert("FLASH: Flashing new firmware\n");
326 pr_alert("FLASH: Image is %u bytes\n", image_data.size);
327 pr_alert("FLASH: Performing flash and reboot/shutdown\n");
328 pr_alert("FLASH: This will take several minutes. Do not power off!\n");
330 /* Small delay to help getting the above message out */
333 /* Return secondary CPUs to firmware */
334 cpumask_copy(&mask, cpu_online_mask);
335 cpumask_clear_cpu(smp_processor_id(), &mask);
336 if (!cpumask_empty(&mask))
337 smp_call_function_many(&mask,
338 flash_return_cpu, NULL, false);
339 /* Hard disable interrupts */
344 * Show candidate image status
346 static ssize_t update_show(struct kobject *kobj,
347 struct kobj_attribute *attr, char *buf)
349 struct update_flash_t *const args_buf = &update_flash_data;
350 return sprintf(buf, "%d\n", args_buf->status);
354 * Set update image flag
355 * 1 - Flash new image
356 * 0 - Cancel flash request
358 static ssize_t update_store(struct kobject *kobj,
359 struct kobj_attribute *attr,
360 const char *buf, size_t count)
362 struct update_flash_t *const args_buf = &update_flash_data;
365 mutex_lock(&image_data_mutex);
369 if (args_buf->status == FLASH_IMG_READY)
370 opal_flash_update(FLASH_UPDATE_CANCEL);
371 args_buf->status = FLASH_NO_OP;
374 /* Image is loaded? */
375 if (image_data.status == IMAGE_READY)
377 opal_flash_update(FLASH_UPDATE_INIT);
379 args_buf->status = FLASH_INVALID_IMG;
385 mutex_unlock(&image_data_mutex);
392 static void free_image_buf(void)
397 addr = image_data.data;
398 size = PAGE_ALIGN(image_data.size);
400 ClearPageReserved(vmalloc_to_page(addr));
404 vfree(image_data.data);
405 image_data.data = NULL;
406 image_data.status = IMAGE_INVALID;
410 * Allocate image buffer.
412 static int alloc_image_buf(char *buffer, size_t count)
417 if (count < sizeof(struct image_header_t)) {
418 pr_warn("FLASH: Invalid candidate image\n");
422 memcpy(&image_header, (void *)buffer, sizeof(struct image_header_t));
423 image_data.size = be32_to_cpu(image_header.size);
424 pr_debug("FLASH: Candidate image size = %u\n", image_data.size);
426 if (image_data.size > MAX_IMAGE_SIZE) {
427 pr_warn("FLASH: Too large image\n");
430 if (image_data.size < VALIDATE_BUF_SIZE) {
431 pr_warn("FLASH: Image is shorter than expected\n");
435 image_data.data = vzalloc(PAGE_ALIGN(image_data.size));
436 if (!image_data.data) {
437 pr_err("%s : Failed to allocate memory\n", __func__);
442 addr = image_data.data;
443 size = PAGE_ALIGN(image_data.size);
445 SetPageReserved(vmalloc_to_page(addr));
450 image_data.status = IMAGE_LOADING;
455 * Copy candidate image
457 * Parse candidate image header to get total image size
458 * and pre-allocate required memory.
460 static ssize_t image_data_write(struct file *filp, struct kobject *kobj,
461 struct bin_attribute *bin_attr,
462 char *buffer, loff_t pos, size_t count)
466 mutex_lock(&image_data_mutex);
470 /* Free memory, if already allocated */
474 /* Cancel outstanding image update request */
475 if (update_flash_data.status == FLASH_IMG_READY)
476 opal_flash_update(FLASH_UPDATE_CANCEL);
478 /* Allocate memory */
479 rc = alloc_image_buf(buffer, count);
484 if (image_data.status != IMAGE_LOADING) {
489 if ((pos + count) > image_data.size) {
494 memcpy(image_data.data + pos, (void *)buffer, count);
497 /* Set image status */
498 if ((pos + count) == image_data.size) {
499 pr_debug("FLASH: Candidate image loaded....\n");
500 image_data.status = IMAGE_READY;
504 mutex_unlock(&image_data_mutex);
510 * OPAL uses below sysfs files for code update.
511 * We create these files under /sys/firmware/opal.
513 * image : Interface to load candidate firmware image
514 * validate_flash : Validate firmware image
515 * manage_flash : Commit/Reject firmware image
516 * update_flash : Flash new firmware image
519 static struct bin_attribute image_data_attr = {
520 .attr = {.name = "image", .mode = 0200},
521 .size = MAX_IMAGE_SIZE, /* Limit image size */
522 .write = image_data_write,
525 static struct kobj_attribute validate_attribute =
526 __ATTR(validate_flash, 0600, validate_show, validate_store);
528 static struct kobj_attribute manage_attribute =
529 __ATTR(manage_flash, 0600, manage_show, manage_store);
531 static struct kobj_attribute update_attribute =
532 __ATTR(update_flash, 0600, update_show, update_store);
534 static struct attribute *image_op_attrs[] = {
535 &validate_attribute.attr,
536 &manage_attribute.attr,
537 &update_attribute.attr,
538 NULL /* need to NULL terminate the list of attributes */
541 static struct attribute_group image_op_attr_group = {
542 .attrs = image_op_attrs,
545 void __init opal_flash_init(void)
549 /* Allocate validate image buffer */
550 validate_flash_data.buf = kzalloc(VALIDATE_BUF_SIZE, GFP_KERNEL);
551 if (!validate_flash_data.buf) {
552 pr_err("%s : Failed to allocate memory\n", __func__);
556 /* Make sure /sys/firmware/opal directory is created */
558 pr_warn("FLASH: opal kobject is not available\n");
562 /* Create the sysfs files */
563 ret = sysfs_create_group(opal_kobj, &image_op_attr_group);
565 pr_warn("FLASH: Failed to create sysfs files\n");
569 ret = sysfs_create_bin_file(opal_kobj, &image_data_attr);
571 pr_warn("FLASH: Failed to create sysfs files\n");
575 /* Set default status */
576 validate_flash_data.status = FLASH_NO_OP;
577 manage_flash_data.status = FLASH_NO_OP;
578 update_flash_data.status = FLASH_NO_OP;
579 image_data.status = IMAGE_INVALID;
583 sysfs_remove_group(opal_kobj, &image_op_attr_group);
586 kfree(validate_flash_data.buf);