Mayank Grover | 98c4c74 | 2019-04-25 17:21:37 +0530 | [diff] [blame] | 1 | /* Copyright (c) 2010-2017,2019 The Linux Foundation. All rights reserved. |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 2 | |
| 3 | * Redistribution and use in source and binary forms, with or without |
| 4 | * modification, are permitted provided that the following conditions are |
| 5 | * met: |
| 6 | * * Redistributions of source code must retain the above copyright |
| 7 | * notice, this list of conditions and the following disclaimer. |
| 8 | * * Redistributions in binary form must reproduce the above |
| 9 | * copyright notice, this list of conditions and the following |
| 10 | * disclaimer in the documentation and/or other materials provided |
| 11 | * with the distribution. |
Duy Truong | f3ac7b3 | 2013-02-13 01:07:28 -0800 | [diff] [blame] | 12 | * * Neither the name of The Linux Foundation nor the names of its |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 13 | * contributors may be used to endorse or promote products derived |
| 14 | * from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 20 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 23 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 24 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 25 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 26 | * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | #include <debug.h> |
| 30 | #include <arch/arm.h> |
| 31 | #include <dev/udc.h> |
| 32 | #include <string.h> |
| 33 | #include <kernel/thread.h> |
| 34 | #include <arch/ops.h> |
Sundarajan Srinivasan | 151c04d | 2014-07-17 15:52:12 -0700 | [diff] [blame] | 35 | #include <arch/defines.h> |
| 36 | #include <malloc.h> |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 37 | |
| 38 | #include <dev/flash.h> |
| 39 | #include <lib/ptable.h> |
| 40 | #include <dev/keys.h> |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 41 | #include <platform.h> |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 42 | #include <target.h> |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 43 | #include <partition_parser.h> |
Greg Grisco | d2471ef | 2011-07-14 13:00:42 -0700 | [diff] [blame] | 44 | #include <mmc.h> |
Unnati Gandhi | 0c8e7c5 | 2014-07-17 14:33:09 +0530 | [diff] [blame] | 45 | #include <malloc.h> |
Mayank Grover | 7e40ad2 | 2017-11-13 17:17:53 +0530 | [diff] [blame] | 46 | #include <stdlib.h> |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 47 | #include "recovery.h" |
| 48 | #include "bootimg.h" |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 49 | #include "smem.h" |
| 50 | |
| 51 | #define BOOT_FLAGS 1 |
| 52 | #define UPDATE_STATUS 2 |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 53 | |
| 54 | static const int MISC_PAGES = 3; // number of pages to save |
| 55 | static const int MISC_COMMAND_PAGE = 1; // bootloader command is this page |
| 56 | static char buf[4096]; |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 57 | |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 58 | unsigned boot_into_recovery = 0; |
| 59 | |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 60 | extern uint32_t get_page_size(); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 61 | extern void reset_device_info(); |
| 62 | extern void set_device_root(); |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 63 | |
| 64 | int get_recovery_message(struct recovery_message *out) |
| 65 | { |
| 66 | struct ptentry *ptn; |
| 67 | struct ptable *ptable; |
| 68 | unsigned offset = 0; |
| 69 | unsigned pagesize = flash_page_size(); |
| 70 | |
| 71 | ptable = flash_get_ptable(); |
| 72 | |
| 73 | if (ptable == NULL) { |
| 74 | dprintf(CRITICAL, "ERROR: Partition table not found\n"); |
| 75 | return -1; |
| 76 | } |
| 77 | ptn = ptable_find(ptable, "misc"); |
| 78 | |
| 79 | if (ptn == NULL) { |
| 80 | dprintf(CRITICAL, "ERROR: No misc partition found\n"); |
| 81 | return -1; |
| 82 | } |
| 83 | |
| 84 | offset += (pagesize * MISC_COMMAND_PAGE); |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 85 | if (flash_read(ptn, offset, (void *) buf, pagesize)) { |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 86 | dprintf(CRITICAL, "ERROR: Cannot read recovery_header\n"); |
| 87 | return -1; |
| 88 | } |
| 89 | memcpy(out, buf, sizeof(*out)); |
| 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | int set_recovery_message(const struct recovery_message *in) |
| 94 | { |
| 95 | struct ptentry *ptn; |
| 96 | struct ptable *ptable; |
| 97 | unsigned offset = 0; |
| 98 | unsigned pagesize = flash_page_size(); |
| 99 | unsigned n = 0; |
Pavel Nedev | 5d4a705 | 2013-05-13 15:14:45 +0300 | [diff] [blame] | 100 | void *scratch_addr = target_get_scratch_address(); |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 101 | |
| 102 | ptable = flash_get_ptable(); |
| 103 | |
| 104 | if (ptable == NULL) { |
| 105 | dprintf(CRITICAL, "ERROR: Partition table not found\n"); |
| 106 | return -1; |
| 107 | } |
| 108 | ptn = ptable_find(ptable, "misc"); |
| 109 | |
| 110 | if (ptn == NULL) { |
| 111 | dprintf(CRITICAL, "ERROR: No misc partition found\n"); |
| 112 | return -1; |
| 113 | } |
| 114 | |
| 115 | n = pagesize * (MISC_COMMAND_PAGE + 1); |
| 116 | |
Pavel Nedev | 5d4a705 | 2013-05-13 15:14:45 +0300 | [diff] [blame] | 117 | if (flash_read(ptn, offset, scratch_addr, n)) { |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 118 | dprintf(CRITICAL, "ERROR: Cannot read recovery_header\n"); |
| 119 | return -1; |
| 120 | } |
| 121 | |
| 122 | offset += (pagesize * MISC_COMMAND_PAGE); |
Pavel Nedev | 5d4a705 | 2013-05-13 15:14:45 +0300 | [diff] [blame] | 123 | offset += (unsigned) scratch_addr; |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 124 | memcpy((void *) offset, in, sizeof(*in)); |
Pavel Nedev | 5d4a705 | 2013-05-13 15:14:45 +0300 | [diff] [blame] | 125 | if (flash_write(ptn, 0, scratch_addr, n)) { |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 126 | dprintf(CRITICAL, "ERROR: flash write fail!\n"); |
| 127 | return -1; |
| 128 | } |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 129 | return 0; |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 130 | } |
| 131 | |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 132 | static int set_ssd_radio_update (char *name) |
| 133 | { |
| 134 | struct ptentry *ptn; |
| 135 | struct ptable *ptable; |
Mayank Grover | ba853de | 2017-06-15 16:20:18 +0530 | [diff] [blame] | 136 | unsigned int *ssd_cookie; |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 137 | unsigned pagesize = flash_page_size(); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 138 | |
| 139 | ptable = flash_get_ptable(); |
| 140 | if (ptable == NULL) { |
| 141 | dprintf(CRITICAL, "ERROR: Partition table not found\n"); |
| 142 | return -1; |
| 143 | } |
| 144 | |
Mayank Grover | ba853de | 2017-06-15 16:20:18 +0530 | [diff] [blame] | 145 | ssd_cookie = malloc(pagesize); |
| 146 | if (!ssd_cookie){ |
| 147 | dprintf(CRITICAL, "ERROR: Memory allocation failure\n"); |
| 148 | return -1; |
| 149 | } |
| 150 | memset(ssd_cookie, 0, pagesize); |
| 151 | ssd_cookie[0] = 0x53534443; |
| 152 | ssd_cookie[1] = 0x4F4F4B49; |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 153 | |
| 154 | ptn = ptable_find(ptable, name); |
| 155 | if (ptn == NULL) { |
| 156 | dprintf(CRITICAL, "ERROR: No %s partition found\n", name); |
Mayank Grover | ba853de | 2017-06-15 16:20:18 +0530 | [diff] [blame] | 157 | goto out; |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Mayank Grover | ba853de | 2017-06-15 16:20:18 +0530 | [diff] [blame] | 160 | if (flash_write(ptn, 0, ssd_cookie, pagesize)) { |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 161 | dprintf(CRITICAL, "ERROR: flash write fail!\n"); |
Mayank Grover | ba853de | 2017-06-15 16:20:18 +0530 | [diff] [blame] | 162 | goto out; |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Mayank Grover | ba853de | 2017-06-15 16:20:18 +0530 | [diff] [blame] | 165 | free(ssd_cookie); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 166 | dprintf(INFO, "FOTA partition written successfully!"); |
| 167 | return 0; |
Mayank Grover | ba853de | 2017-06-15 16:20:18 +0530 | [diff] [blame] | 168 | out: |
| 169 | free(ssd_cookie); |
| 170 | return -1; |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | int get_boot_info_apps (char type, unsigned int *status) |
| 174 | { |
| 175 | boot_info_for_apps apps_boot_info; |
| 176 | int ret = 0; |
| 177 | |
| 178 | ret = smem_read_alloc_entry(SMEM_BOOT_INFO_FOR_APPS, |
| 179 | &apps_boot_info, sizeof(apps_boot_info)); |
| 180 | if (ret) |
| 181 | { |
| 182 | dprintf(CRITICAL, "ERROR: unable to read shared memory for apps boot info %d\n",ret); |
| 183 | return ret; |
| 184 | } |
| 185 | |
| 186 | dprintf(INFO,"boot flag %x update status %x\n",apps_boot_info.boot_flags, |
| 187 | apps_boot_info.status.update_status); |
| 188 | |
| 189 | if(type == BOOT_FLAGS) |
| 190 | *status = apps_boot_info.boot_flags; |
| 191 | else if(type == UPDATE_STATUS) |
| 192 | *status = apps_boot_info.status.update_status; |
| 193 | |
| 194 | return ret; |
| 195 | } |
| 196 | |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 197 | /* Bootloader / Recovery Flow |
| 198 | * |
| 199 | * On every boot, the bootloader will read the recovery_message |
| 200 | * from flash and check the command field. The bootloader should |
| 201 | * deal with the command field not having a 0 terminator correctly |
| 202 | * (so as to not crash if the block is invalid or corrupt). |
| 203 | * |
| 204 | * The bootloader will have to publish the partition that contains |
| 205 | * the recovery_message to the linux kernel so it can update it. |
| 206 | * |
| 207 | * if command == "boot-recovery" -> boot recovery.img |
| 208 | * else if command == "update-radio" -> update radio image (below) |
| 209 | * else -> boot boot.img (normal boot) |
| 210 | * |
| 211 | * Radio Update Flow |
| 212 | * 1. the bootloader will attempt to load and validate the header |
| 213 | * 2. if the header is invalid, status="invalid-update", goto #8 |
| 214 | * 3. display the busy image on-screen |
| 215 | * 4. if the update image is invalid, status="invalid-radio-image", goto #8 |
| 216 | * 5. attempt to update the firmware (depending on the command) |
| 217 | * 6. if successful, status="okay", goto #8 |
| 218 | * 7. if failed, and the old image can still boot, status="failed-update" |
| 219 | * 8. write the recovery_message, leaving the recovery field |
| 220 | * unchanged, updating status, and setting command to |
| 221 | * "boot-recovery" |
| 222 | * 9. reboot |
| 223 | * |
| 224 | * The bootloader will not modify or erase the cache partition. |
| 225 | * It is recovery's responsibility to clean up the mess afterwards. |
| 226 | */ |
| 227 | |
| 228 | int recovery_init (void) |
| 229 | { |
| 230 | struct recovery_message msg; |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 231 | char partition_name[32]; |
| 232 | unsigned valid_command = 0; |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 233 | int update_status = 0; |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 234 | |
| 235 | // get recovery message |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 236 | if (get_recovery_message(&msg)) |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 237 | return -1; |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 238 | msg.command[sizeof(msg.command)-1] = '\0'; //Ensure termination |
Pavel Nedev | 96a9aea | 2013-02-26 15:16:26 -0800 | [diff] [blame] | 239 | if (msg.command[0] != 0 && msg.command[0] != 255) { |
| 240 | dprintf(INFO,"Recovery command: %d %s\n", |
| 241 | sizeof(msg.command), msg.command); |
| 242 | } |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 243 | |
Mayank Grover | 98c4c74 | 2019-04-25 17:21:37 +0530 | [diff] [blame] | 244 | if (!strcmp(RECOVERY_BOOT_RECOVERY_CMD, msg.command)) |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 245 | { |
| 246 | if(!strcmp("RADIO",msg.status)) |
| 247 | { |
| 248 | /* We're now here due to radio update, so check for update status */ |
Greg Grisco | d2471ef | 2011-07-14 13:00:42 -0700 | [diff] [blame] | 249 | int ret = get_boot_info_apps(UPDATE_STATUS, (unsigned int *) &update_status); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 250 | |
| 251 | if(!ret && (update_status & 0x01)) |
| 252 | { |
| 253 | dprintf(INFO,"radio update success\n"); |
Ajay Dudani | f63d02f | 2011-10-01 08:29:53 -0700 | [diff] [blame] | 254 | strlcpy(msg.status, "OKAY", sizeof(msg.status)); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 255 | } |
| 256 | else |
| 257 | { |
| 258 | dprintf(INFO,"radio update failed\n"); |
Ajay Dudani | f63d02f | 2011-10-01 08:29:53 -0700 | [diff] [blame] | 259 | strlcpy(msg.status, "failed-update", sizeof(msg.status)); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 260 | } |
Ajay Dudani | f63d02f | 2011-10-01 08:29:53 -0700 | [diff] [blame] | 261 | strlcpy(msg.command, "", sizeof(msg.command)); // clearing recovery command |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 262 | set_recovery_message(&msg); // send recovery message |
| 263 | boot_into_recovery = 1; // Boot in recovery mode |
| 264 | return 0; |
| 265 | } |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 266 | boot_into_recovery = 1; // Boot in recovery mode |
| 267 | return 0; |
| 268 | } |
| 269 | |
Mayank Grover | 98c4c74 | 2019-04-25 17:21:37 +0530 | [diff] [blame] | 270 | if (target_dynamic_partition_supported() && |
| 271 | !strcmp(RECOVERY_BOOT_FASTBOOT_CMD, msg.command)) { |
| 272 | boot_into_recovery = 1; // Boot in userspace fastboot mode |
| 273 | return 0; |
| 274 | } |
| 275 | |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 276 | if (!strcmp("update-radio",msg.command)) { |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 277 | dprintf(INFO,"start radio update\n"); |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 278 | valid_command = 1; |
Ajay Dudani | f63d02f | 2011-10-01 08:29:53 -0700 | [diff] [blame] | 279 | strlcpy(partition_name, "FOTA", sizeof(partition_name)); |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | //Todo: Add support for bootloader update too. |
| 283 | |
| 284 | if(!valid_command) { |
| 285 | //We need not to do anything |
| 286 | return 0; // Boot in normal mode |
| 287 | } |
| 288 | |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 289 | if (set_ssd_radio_update(partition_name)) { |
| 290 | /* If writing to FOTA partition fails */ |
Ajay Dudani | f63d02f | 2011-10-01 08:29:53 -0700 | [diff] [blame] | 291 | strlcpy(msg.command, "", sizeof(msg.command)); |
| 292 | strlcpy(msg.status, "failed-update", sizeof(msg.status)); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 293 | goto SEND_RECOVERY_MSG; |
| 294 | } |
| 295 | else { |
| 296 | /* Setting this to check the radio update status */ |
Ajay Dudani | f63d02f | 2011-10-01 08:29:53 -0700 | [diff] [blame] | 297 | strlcpy(msg.command, "boot-recovery", sizeof(msg.command)); |
Ajay Dudani | f4caa94 | 2011-10-02 08:57:15 -0700 | [diff] [blame] | 298 | strlcpy(msg.status, "RADIO", sizeof(msg.status)); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 299 | goto SEND_RECOVERY_MSG; |
| 300 | } |
Ajay Dudani | f63d02f | 2011-10-01 08:29:53 -0700 | [diff] [blame] | 301 | strlcpy(msg.status, "OKAY", sizeof(msg.status)); |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 302 | |
| 303 | SEND_RECOVERY_MSG: |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 304 | set_recovery_message(&msg); // send recovery message |
| 305 | boot_into_recovery = 1; // Boot in recovery mode |
| 306 | reboot_device(0); |
| 307 | return 0; |
| 308 | } |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 309 | |
| 310 | static int emmc_set_recovery_msg(struct recovery_message *out) |
| 311 | { |
| 312 | char *ptn_name = "misc"; |
| 313 | unsigned long long ptn = 0; |
Mayank Grover | 9db9724 | 2017-06-16 12:41:56 +0530 | [diff] [blame] | 314 | unsigned blocksize = mmc_get_device_blocksize(); |
| 315 | unsigned int size = ROUND_TO_PAGE(sizeof(*out), (unsigned)blocksize - 1); |
| 316 | unsigned char *data = NULL; |
| 317 | int ret = 0; |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 318 | int index = INVALID_PTN; |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 319 | |
Mayank Grover | 9db9724 | 2017-06-16 12:41:56 +0530 | [diff] [blame] | 320 | data = malloc(size); |
| 321 | if(!data) |
| 322 | { |
| 323 | dprintf(CRITICAL,"memory allocation error \n"); |
| 324 | ret = -1; |
| 325 | goto out; |
| 326 | } |
| 327 | |
Unnati Gandhi | 0c8e7c5 | 2014-07-17 14:33:09 +0530 | [diff] [blame] | 328 | index = partition_get_index((const char *) ptn_name); |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 329 | ptn = partition_get_offset(index); |
Channagoud Kadabi | 5d0371c | 2014-10-21 22:27:07 -0700 | [diff] [blame] | 330 | mmc_set_lun(partition_get_lun(index)); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 331 | if(ptn == 0) { |
| 332 | dprintf(CRITICAL,"partition %s doesn't exist\n",ptn_name); |
Mayank Grover | 9db9724 | 2017-06-16 12:41:56 +0530 | [diff] [blame] | 333 | ret = -1; |
| 334 | goto out; |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 335 | } |
Mayank Grover | 9db9724 | 2017-06-16 12:41:56 +0530 | [diff] [blame] | 336 | memset(data, 0, size); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 337 | memcpy(data, out, sizeof(*out)); |
| 338 | if (mmc_write(ptn , size, (unsigned int*)data)) { |
| 339 | dprintf(CRITICAL,"mmc write failure %s %d\n",ptn_name, sizeof(*out)); |
Mayank Grover | 9db9724 | 2017-06-16 12:41:56 +0530 | [diff] [blame] | 340 | ret = -1; |
| 341 | goto out; |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 342 | } |
Mayank Grover | 9db9724 | 2017-06-16 12:41:56 +0530 | [diff] [blame] | 343 | out: |
| 344 | if (data) |
| 345 | free(data); |
| 346 | return ret; |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | static int emmc_get_recovery_msg(struct recovery_message *in) |
| 350 | { |
| 351 | char *ptn_name = "misc"; |
| 352 | unsigned long long ptn = 0; |
Sundarajan Srinivasan | 151c04d | 2014-07-17 15:52:12 -0700 | [diff] [blame] | 353 | unsigned int size; |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 354 | int index = INVALID_PTN; |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 355 | |
Sundarajan Srinivasan | 151c04d | 2014-07-17 15:52:12 -0700 | [diff] [blame] | 356 | size = mmc_get_device_blocksize(); |
Unnati Gandhi | 0c8e7c5 | 2014-07-17 14:33:09 +0530 | [diff] [blame] | 357 | index = partition_get_index((const char *) ptn_name); |
Channagoud Kadabi | f280563 | 2015-01-07 16:46:50 -0800 | [diff] [blame] | 358 | if (index < 0) |
| 359 | { |
| 360 | dprintf(CRITICAL, "%s: Partition not found\n", ptn_name); |
| 361 | return -1; |
| 362 | } |
| 363 | |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 364 | ptn = partition_get_offset(index); |
Channagoud Kadabi | 5d0371c | 2014-10-21 22:27:07 -0700 | [diff] [blame] | 365 | mmc_set_lun(partition_get_lun(index)); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 366 | if(ptn == 0) { |
| 367 | dprintf(CRITICAL,"partition %s doesn't exist\n",ptn_name); |
| 368 | return -1; |
| 369 | } |
Sundarajan Srinivasan | 151c04d | 2014-07-17 15:52:12 -0700 | [diff] [blame] | 370 | if (mmc_read(ptn , (unsigned int*)in, size)) { |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 371 | dprintf(CRITICAL,"mmc read failure %s %d\n",ptn_name, size); |
| 372 | return -1; |
| 373 | } |
Sundarajan Srinivasan | 151c04d | 2014-07-17 15:52:12 -0700 | [diff] [blame] | 374 | |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 375 | return 0; |
| 376 | } |
| 377 | |
Mayank Grover | 98c4c74 | 2019-04-25 17:21:37 +0530 | [diff] [blame] | 378 | /* Generic funcition to write misc commands. */ |
| 379 | int send_recovery_cmd(const char *command) |
| 380 | { |
| 381 | struct recovery_message msg; |
| 382 | int status = 0; |
| 383 | memset(&msg, 0, sizeof(msg)); |
| 384 | |
| 385 | /* Populate command to msg */ |
| 386 | snprintf(msg.command, |
| 387 | sizeof(msg.command), |
| 388 | command); |
| 389 | |
| 390 | dprintf(INFO,"Recovery command: %s\n", msg.command); |
| 391 | if (target_is_emmc_boot()) |
| 392 | /* Update emmc partition */ |
| 393 | status = emmc_set_recovery_msg(&msg); |
| 394 | else |
| 395 | status = set_recovery_message(&msg); |
| 396 | |
| 397 | return status; |
| 398 | } |
| 399 | |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 400 | int _emmc_recovery_init(void) |
| 401 | { |
| 402 | int update_status = 0; |
Sundarajan Srinivasan | 151c04d | 2014-07-17 15:52:12 -0700 | [diff] [blame] | 403 | struct recovery_message *msg; |
| 404 | uint32_t block_size = 0; |
| 405 | |
| 406 | block_size = mmc_get_device_blocksize(); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 407 | |
| 408 | // get recovery message |
Sundarajan Srinivasan | 151c04d | 2014-07-17 15:52:12 -0700 | [diff] [blame] | 409 | msg = (struct recovery_message *)memalign(CACHE_LINE, block_size); |
| 410 | ASSERT(msg); |
| 411 | |
| 412 | if(emmc_get_recovery_msg(msg)) |
| 413 | { |
| 414 | if(msg) |
| 415 | free(msg); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 416 | return -1; |
Pavel Nedev | 96a9aea | 2013-02-26 15:16:26 -0800 | [diff] [blame] | 417 | } |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 418 | |
Sundarajan Srinivasan | 151c04d | 2014-07-17 15:52:12 -0700 | [diff] [blame] | 419 | msg->command[sizeof(msg->command)-1] = '\0'; //Ensure termination |
| 420 | if (msg->command[0] != 0 && msg->command[0] != 255) { |
| 421 | dprintf(INFO,"Recovery command: %d %s\n", |
| 422 | sizeof(msg->command), msg->command); |
| 423 | } |
| 424 | |
Mayank Grover | 98c4c74 | 2019-04-25 17:21:37 +0530 | [diff] [blame] | 425 | if (!strcmp(msg->command, RECOVERY_BOOT_RECOVERY_CMD)) { |
| 426 | boot_into_recovery = 1; |
| 427 | } |
| 428 | |
| 429 | if (target_dynamic_partition_supported() && |
| 430 | !strcmp(msg->command, RECOVERY_BOOT_FASTBOOT_CMD)) { |
Stanimir Varbanov | be04199 | 2013-04-26 14:29:21 +0300 | [diff] [blame] | 431 | boot_into_recovery = 1; |
| 432 | } |
| 433 | |
Sundarajan Srinivasan | 151c04d | 2014-07-17 15:52:12 -0700 | [diff] [blame] | 434 | if (!strcmp("update-radio",msg->command)) |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 435 | { |
| 436 | /* We're now here due to radio update, so check for update status */ |
Greg Grisco | d2471ef | 2011-07-14 13:00:42 -0700 | [diff] [blame] | 437 | int ret = get_boot_info_apps(UPDATE_STATUS, (unsigned int *) &update_status); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 438 | |
| 439 | if(!ret && (update_status & 0x01)) |
| 440 | { |
| 441 | dprintf(INFO,"radio update success\n"); |
Sundarajan Srinivasan | 151c04d | 2014-07-17 15:52:12 -0700 | [diff] [blame] | 442 | strlcpy(msg->status, "OKAY", sizeof(msg->status)); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 443 | } |
| 444 | else |
| 445 | { |
| 446 | dprintf(INFO,"radio update failed\n"); |
Sundarajan Srinivasan | 151c04d | 2014-07-17 15:52:12 -0700 | [diff] [blame] | 447 | strlcpy(msg->status, "failed-update", sizeof(msg->status)); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 448 | } |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 449 | boot_into_recovery = 1; // Boot in recovery mode |
| 450 | } |
Sundarajan Srinivasan | 151c04d | 2014-07-17 15:52:12 -0700 | [diff] [blame] | 451 | if (!strcmp("reset-device-info",msg->command)) |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 452 | { |
| 453 | reset_device_info(); |
| 454 | } |
Sundarajan Srinivasan | 151c04d | 2014-07-17 15:52:12 -0700 | [diff] [blame] | 455 | if (!strcmp("root-detect",msg->command)) |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 456 | { |
| 457 | set_device_root(); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 458 | } |
| 459 | else |
Sundarajan Srinivasan | 151c04d | 2014-07-17 15:52:12 -0700 | [diff] [blame] | 460 | goto out;// do nothing |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 461 | |
Sundarajan Srinivasan | 151c04d | 2014-07-17 15:52:12 -0700 | [diff] [blame] | 462 | strlcpy(msg->command, "", sizeof(msg->command)); // clearing recovery command |
| 463 | emmc_set_recovery_msg(msg); // send recovery message |
| 464 | |
| 465 | out: |
| 466 | if(msg) |
| 467 | free(msg); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 468 | return 0; |
| 469 | } |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 470 | |
| 471 | static int read_misc(unsigned page_offset, void *buf, unsigned size) |
| 472 | { |
| 473 | const char *ptn_name = "misc"; |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 474 | uint32_t pagesize = get_page_size(); |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 475 | unsigned offset; |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 476 | |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 477 | if (size == 0 || buf == NULL) |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 478 | return -1; |
| 479 | |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 480 | offset = page_offset * pagesize; |
| 481 | |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 482 | if (target_is_emmc_boot()) |
| 483 | { |
| 484 | int index; |
| 485 | unsigned long long ptn; |
| 486 | unsigned long long ptn_size; |
| 487 | |
| 488 | index = partition_get_index(ptn_name); |
| 489 | if (index == INVALID_PTN) |
| 490 | { |
| 491 | dprintf(CRITICAL, "No '%s' partition found\n", ptn_name); |
| 492 | return -1; |
| 493 | } |
| 494 | |
| 495 | ptn = partition_get_offset(index); |
| 496 | ptn_size = partition_get_size(index); |
| 497 | |
Channagoud Kadabi | 5d0371c | 2014-10-21 22:27:07 -0700 | [diff] [blame] | 498 | mmc_set_lun(partition_get_lun(index)); |
| 499 | |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 500 | if (ptn_size < offset + size) |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 501 | { |
| 502 | dprintf(CRITICAL, "Read request out of '%s' boundaries\n", |
| 503 | ptn_name); |
| 504 | return -1; |
| 505 | } |
| 506 | |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 507 | if (mmc_read(ptn + offset, (unsigned int *)buf, size)) |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 508 | { |
| 509 | dprintf(CRITICAL, "Reading MMC failed\n"); |
| 510 | return -1; |
| 511 | } |
| 512 | } |
| 513 | else |
| 514 | { |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 515 | dprintf(CRITICAL, "Misc partition not supported for NAND targets.\n"); |
| 516 | return -1; |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 517 | } |
| 518 | |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 519 | return 0; |
| 520 | } |
| 521 | |
Pavel Nedev | c728bb3 | 2013-04-05 16:58:05 +0300 | [diff] [blame] | 522 | int write_misc(unsigned page_offset, void *buf, unsigned size) |
| 523 | { |
| 524 | const char *ptn_name = "misc"; |
| 525 | void *scratch_addr = target_get_scratch_address(); |
| 526 | unsigned offset; |
| 527 | unsigned aligned_size; |
| 528 | |
| 529 | if (size == 0 || buf == NULL || scratch_addr == NULL) |
| 530 | return -1; |
| 531 | |
| 532 | if (target_is_emmc_boot()) |
| 533 | { |
| 534 | int index; |
| 535 | unsigned long long ptn; |
| 536 | unsigned long long ptn_size; |
| 537 | |
| 538 | index = partition_get_index(ptn_name); |
| 539 | if (index == INVALID_PTN) |
| 540 | { |
| 541 | dprintf(CRITICAL, "No '%s' partition found\n", ptn_name); |
| 542 | return -1; |
| 543 | } |
| 544 | |
| 545 | ptn = partition_get_offset(index); |
| 546 | ptn_size = partition_get_size(index); |
| 547 | |
| 548 | offset = page_offset * BLOCK_SIZE; |
| 549 | aligned_size = ROUND_TO_PAGE(size, (unsigned)BLOCK_SIZE - 1); |
| 550 | if (ptn_size < offset + aligned_size) |
| 551 | { |
| 552 | dprintf(CRITICAL, "Write request out of '%s' boundaries\n", |
| 553 | ptn_name); |
| 554 | return -1; |
| 555 | } |
| 556 | |
Mayank Grover | 467bf49 | 2017-10-24 12:27:53 +0530 | [diff] [blame] | 557 | /* This will ensure, we zeored out any extra bytes |
| 558 | we will push to emmc, to prevent information leak */ |
| 559 | if (aligned_size > size) |
| 560 | memset((scratch_addr + size), 0, (aligned_size-size)); |
| 561 | |
Pavel Nedev | c728bb3 | 2013-04-05 16:58:05 +0300 | [diff] [blame] | 562 | if (scratch_addr != buf) |
| 563 | memcpy(scratch_addr, buf, size); |
Channagoud Kadabi | 2776b65 | 2015-08-28 15:41:26 -0700 | [diff] [blame] | 564 | |
| 565 | /* Set Lun for misc partition */ |
| 566 | mmc_set_lun(partition_get_lun(index)); |
| 567 | |
Pavel Nedev | c728bb3 | 2013-04-05 16:58:05 +0300 | [diff] [blame] | 568 | if (mmc_write(ptn + offset, aligned_size, (unsigned int *)scratch_addr)) |
| 569 | { |
| 570 | dprintf(CRITICAL, "Writing MMC failed\n"); |
| 571 | return -1; |
| 572 | } |
| 573 | } |
| 574 | else |
| 575 | { |
| 576 | struct ptentry *ptn; |
| 577 | struct ptable *ptable; |
| 578 | unsigned pagesize = flash_page_size(); |
| 579 | |
| 580 | ptable = flash_get_ptable(); |
| 581 | if (ptable == NULL) |
| 582 | { |
| 583 | dprintf(CRITICAL, "Partition table not found\n"); |
| 584 | return -1; |
| 585 | } |
| 586 | |
| 587 | ptn = ptable_find(ptable, ptn_name); |
| 588 | if (ptn == NULL) |
| 589 | { |
| 590 | dprintf(CRITICAL, "No '%s' partition found\n", ptn_name); |
| 591 | return -1; |
| 592 | } |
| 593 | |
| 594 | offset = page_offset * pagesize; |
| 595 | aligned_size = ROUND_TO_PAGE(size, pagesize - 1); |
| 596 | if (ptn->length < offset + aligned_size) |
| 597 | { |
| 598 | dprintf(CRITICAL, "Write request out of '%s' boundaries\n", |
| 599 | ptn_name); |
| 600 | return -1; |
| 601 | } |
| 602 | |
Mayank Grover | 467bf49 | 2017-10-24 12:27:53 +0530 | [diff] [blame] | 603 | /* This will ensure, we zeored out any extra bytes |
| 604 | we will push, to prevent information leak */ |
| 605 | if (aligned_size > size) |
| 606 | memset((scratch_addr + size), 0, (aligned_size-size)); |
| 607 | |
Pavel Nedev | c728bb3 | 2013-04-05 16:58:05 +0300 | [diff] [blame] | 608 | if (scratch_addr != buf) |
| 609 | memcpy(scratch_addr, buf, size); |
Mayank Grover | 467bf49 | 2017-10-24 12:27:53 +0530 | [diff] [blame] | 610 | |
Pavel Nedev | c728bb3 | 2013-04-05 16:58:05 +0300 | [diff] [blame] | 611 | if (flash_write(ptn, offset, scratch_addr, aligned_size)) { |
| 612 | dprintf(CRITICAL, "Writing flash failed\n"); |
| 613 | return -1; |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | return 0; |
| 618 | } |
| 619 | |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 620 | int get_ffbm(char *ffbm, unsigned size) |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 621 | { |
| 622 | const char *ffbm_cmd = "ffbm-"; |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 623 | uint32_t page_size = get_page_size(); |
| 624 | char *ffbm_page_buffer = NULL; |
| 625 | int retval = 0; |
| 626 | if (size < FFBM_MODE_BUF_SIZE || size >= page_size) |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 627 | { |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 628 | dprintf(CRITICAL, "Invalid size argument passed to get_ffbm\n"); |
| 629 | retval = -1; |
| 630 | goto cleanup; |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 631 | } |
Parth Dixit | 75b1681 | 2016-04-05 19:17:17 +0530 | [diff] [blame] | 632 | ffbm_page_buffer = (char*)memalign(CACHE_LINE, page_size); |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 633 | if (!ffbm_page_buffer) |
| 634 | { |
| 635 | dprintf(CRITICAL, "Failed to alloc buffer for ffbm cookie\n"); |
| 636 | retval = -1; |
| 637 | goto cleanup; |
| 638 | } |
| 639 | if (read_misc(0, ffbm_page_buffer, page_size)) |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 640 | { |
| 641 | dprintf(CRITICAL, "Error reading MISC partition\n"); |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 642 | retval = -1; |
| 643 | goto cleanup; |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 644 | } |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 645 | ffbm_page_buffer[size] = '\0'; |
| 646 | if (strncmp(ffbm_cmd, ffbm_page_buffer, strlen(ffbm_cmd))) |
| 647 | { |
| 648 | retval = 0; |
| 649 | goto cleanup; |
| 650 | } |
| 651 | else |
| 652 | { |
| 653 | if (strlcpy(ffbm, ffbm_page_buffer, size) < |
| 654 | FFBM_MODE_BUF_SIZE -1) |
| 655 | { |
| 656 | dprintf(CRITICAL, "Invalid string in misc partition\n"); |
| 657 | retval = -1; |
| 658 | } |
| 659 | else |
| 660 | retval = 1; |
| 661 | } |
| 662 | cleanup: |
| 663 | if(ffbm_page_buffer) |
| 664 | free(ffbm_page_buffer); |
| 665 | return retval; |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 666 | } |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 667 | |
| 668 | |