Duncan Laurie | e2cea4f | 2015-12-01 19:14:09 -0800 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2015 Google Inc. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 of the License |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | */ |
| 15 | |
| 16 | |
| 17 | #include <console/console.h> |
| 18 | #include <stdint.h> |
| 19 | #include <string.h> |
| 20 | #include <stdlib.h> |
| 21 | #include "ec.h" |
| 22 | #include "ec_commands.h" |
| 23 | |
| 24 | /* |
| 25 | * google_chromeec_vstore_supported - Check if vstore is supported |
| 26 | */ |
| 27 | int google_chromeec_vstore_supported(void) |
| 28 | { |
| 29 | return google_chromeec_check_feature(EC_FEATURE_VSTORE); |
| 30 | } |
| 31 | |
| 32 | /* |
| 33 | * google_chromeec_vstore_info - Query EC for vstore information |
| 34 | * |
| 35 | * Returns the number of vstore slots supported by the EC, with the |
| 36 | * mask of locked slots saved into passed parameter if it is present. |
| 37 | */ |
| 38 | int google_chromeec_vstore_info(uint32_t *locked) |
| 39 | { |
| 40 | struct ec_response_vstore_info info; |
| 41 | struct chromeec_command cmd = { |
| 42 | .cmd_code = EC_CMD_VSTORE_INFO, |
| 43 | .cmd_size_out = sizeof(info), |
| 44 | .cmd_data_out = &info, |
| 45 | }; |
| 46 | |
| 47 | if (google_chromeec_command(&cmd) != 0) |
| 48 | return 0; |
| 49 | |
| 50 | if (locked) |
| 51 | *locked = info.slot_locked; |
| 52 | return info.slot_count; |
| 53 | } |
| 54 | |
| 55 | /* |
| 56 | * google_chromeec_vstore_read - Read data from EC vstore slot |
| 57 | * |
| 58 | * @slot: vstore slot to read from |
| 59 | * @data: buffer to store read data, must be EC_VSTORE_SLOT_SIZE bytes |
| 60 | */ |
| 61 | int google_chromeec_vstore_read(int slot, uint8_t *data) |
| 62 | { |
| 63 | struct ec_params_vstore_read req = { |
| 64 | .slot = slot, |
| 65 | }; |
| 66 | struct chromeec_command cmd = { |
| 67 | .cmd_code = EC_CMD_VSTORE_READ, |
| 68 | .cmd_size_in = sizeof(req), |
| 69 | .cmd_data_in = &req, |
| 70 | .cmd_size_out = EC_VSTORE_SLOT_SIZE, |
| 71 | .cmd_data_out = data, |
| 72 | }; |
| 73 | |
| 74 | if (!data || req.slot >= EC_VSTORE_SLOT_MAX) |
| 75 | return -1; |
| 76 | |
| 77 | return google_chromeec_command(&cmd); |
| 78 | } |
| 79 | |
| 80 | /* |
| 81 | * google_chromeec_vstore_write - Save data into EC vstore slot |
| 82 | * |
| 83 | * @slot: vstore slot to write into |
| 84 | * @data: data to write |
| 85 | * @size: size of data in bytes |
| 86 | * |
| 87 | * Maximum size of data is EC_VSTORE_SLOT_SIZE. It is the callers |
| 88 | * responsibility to check the number of implemented slots by |
| 89 | * querying the vstore info. |
| 90 | */ |
| 91 | int google_chromeec_vstore_write(int slot, uint8_t *data, size_t size) |
| 92 | { |
| 93 | struct ec_params_vstore_write req = { |
| 94 | .slot = slot, |
| 95 | }; |
| 96 | struct chromeec_command cmd = { |
| 97 | .cmd_code = EC_CMD_VSTORE_WRITE, |
| 98 | .cmd_size_in = sizeof(req), |
| 99 | .cmd_data_in = &req, |
| 100 | }; |
| 101 | |
| 102 | if (req.slot >= EC_VSTORE_SLOT_MAX || size > EC_VSTORE_SLOT_SIZE) |
| 103 | return -1; |
| 104 | |
| 105 | memcpy(req.data, data, size); |
| 106 | |
| 107 | return google_chromeec_command(&cmd); |
| 108 | } |