Kishor PK | 8a2783f | 2017-04-20 14:51:04 +0530 | [diff] [blame] | 1 | /* Copyright (c) 2010-2017, 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> |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 46 | |
| 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 |
| 53 | #define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y))) |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 54 | |
| 55 | static const int MISC_PAGES = 3; // number of pages to save |
| 56 | static const int MISC_COMMAND_PAGE = 1; // bootloader command is this page |
| 57 | static char buf[4096]; |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 58 | |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 59 | unsigned boot_into_recovery = 0; |
| 60 | |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 61 | extern uint32_t get_page_size(); |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 62 | extern void reset_device_info(); |
| 63 | extern void set_device_root(); |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 64 | |
| 65 | int get_recovery_message(struct recovery_message *out) |
| 66 | { |
| 67 | struct ptentry *ptn; |
| 68 | struct ptable *ptable; |
| 69 | unsigned offset = 0; |
| 70 | unsigned pagesize = flash_page_size(); |
| 71 | |
| 72 | ptable = flash_get_ptable(); |
| 73 | |
| 74 | if (ptable == NULL) { |
| 75 | dprintf(CRITICAL, "ERROR: Partition table not found\n"); |
| 76 | return -1; |
| 77 | } |
| 78 | ptn = ptable_find(ptable, "misc"); |
| 79 | |
| 80 | if (ptn == NULL) { |
| 81 | dprintf(CRITICAL, "ERROR: No misc partition found\n"); |
| 82 | return -1; |
| 83 | } |
| 84 | |
| 85 | offset += (pagesize * MISC_COMMAND_PAGE); |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 86 | if (flash_read(ptn, offset, (void *) buf, pagesize)) { |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 87 | dprintf(CRITICAL, "ERROR: Cannot read recovery_header\n"); |
| 88 | return -1; |
| 89 | } |
| 90 | memcpy(out, buf, sizeof(*out)); |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | int set_recovery_message(const struct recovery_message *in) |
| 95 | { |
| 96 | struct ptentry *ptn; |
| 97 | struct ptable *ptable; |
| 98 | unsigned offset = 0; |
| 99 | unsigned pagesize = flash_page_size(); |
| 100 | unsigned n = 0; |
Pavel Nedev | 5d4a705 | 2013-05-13 15:14:45 +0300 | [diff] [blame] | 101 | void *scratch_addr = target_get_scratch_address(); |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 102 | |
| 103 | ptable = flash_get_ptable(); |
| 104 | |
| 105 | if (ptable == NULL) { |
| 106 | dprintf(CRITICAL, "ERROR: Partition table not found\n"); |
| 107 | return -1; |
| 108 | } |
| 109 | ptn = ptable_find(ptable, "misc"); |
| 110 | |
| 111 | if (ptn == NULL) { |
| 112 | dprintf(CRITICAL, "ERROR: No misc partition found\n"); |
| 113 | return -1; |
| 114 | } |
| 115 | |
| 116 | n = pagesize * (MISC_COMMAND_PAGE + 1); |
| 117 | |
Pavel Nedev | 5d4a705 | 2013-05-13 15:14:45 +0300 | [diff] [blame] | 118 | if (flash_read(ptn, offset, scratch_addr, n)) { |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 119 | dprintf(CRITICAL, "ERROR: Cannot read recovery_header\n"); |
| 120 | return -1; |
| 121 | } |
| 122 | |
| 123 | offset += (pagesize * MISC_COMMAND_PAGE); |
Pavel Nedev | 5d4a705 | 2013-05-13 15:14:45 +0300 | [diff] [blame] | 124 | offset += (unsigned) scratch_addr; |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 125 | memcpy((void *) offset, in, sizeof(*in)); |
Pavel Nedev | 5d4a705 | 2013-05-13 15:14:45 +0300 | [diff] [blame] | 126 | if (flash_write(ptn, 0, scratch_addr, n)) { |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 127 | dprintf(CRITICAL, "ERROR: flash write fail!\n"); |
| 128 | return -1; |
| 129 | } |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 130 | return 0; |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 131 | } |
| 132 | |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 133 | static int set_ssd_radio_update (char *name) |
| 134 | { |
| 135 | struct ptentry *ptn; |
| 136 | struct ptable *ptable; |
Mayank Grover | ba853de | 2017-06-15 16:20:18 +0530 | [diff] [blame] | 137 | unsigned int *ssd_cookie; |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 138 | unsigned pagesize = flash_page_size(); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 139 | |
| 140 | ptable = flash_get_ptable(); |
| 141 | if (ptable == NULL) { |
| 142 | dprintf(CRITICAL, "ERROR: Partition table not found\n"); |
| 143 | return -1; |
| 144 | } |
| 145 | |
Mayank Grover | ba853de | 2017-06-15 16:20:18 +0530 | [diff] [blame] | 146 | ssd_cookie = malloc(pagesize); |
| 147 | if (!ssd_cookie){ |
| 148 | dprintf(CRITICAL, "ERROR: Memory allocation failure\n"); |
| 149 | return -1; |
| 150 | } |
| 151 | memset(ssd_cookie, 0, pagesize); |
| 152 | ssd_cookie[0] = 0x53534443; |
| 153 | ssd_cookie[1] = 0x4F4F4B49; |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 154 | |
| 155 | ptn = ptable_find(ptable, name); |
| 156 | if (ptn == NULL) { |
| 157 | dprintf(CRITICAL, "ERROR: No %s partition found\n", name); |
Mayank Grover | ba853de | 2017-06-15 16:20:18 +0530 | [diff] [blame] | 158 | goto out; |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Mayank Grover | ba853de | 2017-06-15 16:20:18 +0530 | [diff] [blame] | 161 | if (flash_write(ptn, 0, ssd_cookie, pagesize)) { |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 162 | dprintf(CRITICAL, "ERROR: flash write fail!\n"); |
Mayank Grover | ba853de | 2017-06-15 16:20:18 +0530 | [diff] [blame] | 163 | goto out; |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Mayank Grover | ba853de | 2017-06-15 16:20:18 +0530 | [diff] [blame] | 166 | free(ssd_cookie); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 167 | dprintf(INFO, "FOTA partition written successfully!"); |
| 168 | return 0; |
Mayank Grover | ba853de | 2017-06-15 16:20:18 +0530 | [diff] [blame] | 169 | out: |
| 170 | free(ssd_cookie); |
| 171 | return -1; |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | int get_boot_info_apps (char type, unsigned int *status) |
| 175 | { |
| 176 | boot_info_for_apps apps_boot_info; |
| 177 | int ret = 0; |
| 178 | |
| 179 | ret = smem_read_alloc_entry(SMEM_BOOT_INFO_FOR_APPS, |
| 180 | &apps_boot_info, sizeof(apps_boot_info)); |
| 181 | if (ret) |
| 182 | { |
| 183 | dprintf(CRITICAL, "ERROR: unable to read shared memory for apps boot info %d\n",ret); |
| 184 | return ret; |
| 185 | } |
| 186 | |
| 187 | dprintf(INFO,"boot flag %x update status %x\n",apps_boot_info.boot_flags, |
| 188 | apps_boot_info.status.update_status); |
| 189 | |
| 190 | if(type == BOOT_FLAGS) |
| 191 | *status = apps_boot_info.boot_flags; |
| 192 | else if(type == UPDATE_STATUS) |
| 193 | *status = apps_boot_info.status.update_status; |
| 194 | |
| 195 | return ret; |
| 196 | } |
| 197 | |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 198 | /* Bootloader / Recovery Flow |
| 199 | * |
| 200 | * On every boot, the bootloader will read the recovery_message |
| 201 | * from flash and check the command field. The bootloader should |
| 202 | * deal with the command field not having a 0 terminator correctly |
| 203 | * (so as to not crash if the block is invalid or corrupt). |
| 204 | * |
| 205 | * The bootloader will have to publish the partition that contains |
| 206 | * the recovery_message to the linux kernel so it can update it. |
| 207 | * |
| 208 | * if command == "boot-recovery" -> boot recovery.img |
| 209 | * else if command == "update-radio" -> update radio image (below) |
| 210 | * else -> boot boot.img (normal boot) |
| 211 | * |
| 212 | * Radio Update Flow |
| 213 | * 1. the bootloader will attempt to load and validate the header |
| 214 | * 2. if the header is invalid, status="invalid-update", goto #8 |
| 215 | * 3. display the busy image on-screen |
| 216 | * 4. if the update image is invalid, status="invalid-radio-image", goto #8 |
| 217 | * 5. attempt to update the firmware (depending on the command) |
| 218 | * 6. if successful, status="okay", goto #8 |
| 219 | * 7. if failed, and the old image can still boot, status="failed-update" |
| 220 | * 8. write the recovery_message, leaving the recovery field |
| 221 | * unchanged, updating status, and setting command to |
| 222 | * "boot-recovery" |
| 223 | * 9. reboot |
| 224 | * |
| 225 | * The bootloader will not modify or erase the cache partition. |
| 226 | * It is recovery's responsibility to clean up the mess afterwards. |
| 227 | */ |
| 228 | |
| 229 | int recovery_init (void) |
| 230 | { |
| 231 | struct recovery_message msg; |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 232 | char partition_name[32]; |
| 233 | unsigned valid_command = 0; |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 234 | int update_status = 0; |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 235 | |
| 236 | // get recovery message |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 237 | if (get_recovery_message(&msg)) |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 238 | return -1; |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 239 | msg.command[sizeof(msg.command)-1] = '\0'; //Ensure termination |
Pavel Nedev | 96a9aea | 2013-02-26 15:16:26 -0800 | [diff] [blame] | 240 | if (msg.command[0] != 0 && msg.command[0] != 255) { |
| 241 | dprintf(INFO,"Recovery command: %d %s\n", |
| 242 | sizeof(msg.command), msg.command); |
| 243 | } |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 244 | |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 245 | if (!strcmp("boot-recovery",msg.command)) |
| 246 | { |
| 247 | if(!strcmp("RADIO",msg.status)) |
| 248 | { |
| 249 | /* 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] | 250 | int ret = get_boot_info_apps(UPDATE_STATUS, (unsigned int *) &update_status); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 251 | |
| 252 | if(!ret && (update_status & 0x01)) |
| 253 | { |
| 254 | dprintf(INFO,"radio update success\n"); |
Ajay Dudani | f63d02f | 2011-10-01 08:29:53 -0700 | [diff] [blame] | 255 | strlcpy(msg.status, "OKAY", sizeof(msg.status)); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 256 | } |
| 257 | else |
| 258 | { |
| 259 | dprintf(INFO,"radio update failed\n"); |
Ajay Dudani | f63d02f | 2011-10-01 08:29:53 -0700 | [diff] [blame] | 260 | strlcpy(msg.status, "failed-update", sizeof(msg.status)); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 261 | } |
Ajay Dudani | f63d02f | 2011-10-01 08:29:53 -0700 | [diff] [blame] | 262 | strlcpy(msg.command, "", sizeof(msg.command)); // clearing recovery command |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 263 | set_recovery_message(&msg); // send recovery message |
| 264 | boot_into_recovery = 1; // Boot in recovery mode |
| 265 | return 0; |
| 266 | } |
| 267 | |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 268 | valid_command = 1; |
Ajay Dudani | f63d02f | 2011-10-01 08:29:53 -0700 | [diff] [blame] | 269 | strlcpy(msg.command, "", sizeof(msg.command)); // to safe against multiple reboot into recovery |
| 270 | strlcpy(msg.status, "OKAY", sizeof(msg.status)); |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 271 | set_recovery_message(&msg); // send recovery message |
| 272 | boot_into_recovery = 1; // Boot in recovery mode |
| 273 | return 0; |
| 274 | } |
| 275 | |
| 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 | |
| 378 | int _emmc_recovery_init(void) |
| 379 | { |
| 380 | int update_status = 0; |
Sundarajan Srinivasan | 151c04d | 2014-07-17 15:52:12 -0700 | [diff] [blame] | 381 | struct recovery_message *msg; |
| 382 | uint32_t block_size = 0; |
| 383 | |
| 384 | block_size = mmc_get_device_blocksize(); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 385 | |
| 386 | // get recovery message |
Sundarajan Srinivasan | 151c04d | 2014-07-17 15:52:12 -0700 | [diff] [blame] | 387 | msg = (struct recovery_message *)memalign(CACHE_LINE, block_size); |
| 388 | ASSERT(msg); |
| 389 | |
| 390 | if(emmc_get_recovery_msg(msg)) |
| 391 | { |
| 392 | if(msg) |
| 393 | free(msg); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 394 | return -1; |
Pavel Nedev | 96a9aea | 2013-02-26 15:16:26 -0800 | [diff] [blame] | 395 | } |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 396 | |
Sundarajan Srinivasan | 151c04d | 2014-07-17 15:52:12 -0700 | [diff] [blame] | 397 | msg->command[sizeof(msg->command)-1] = '\0'; //Ensure termination |
| 398 | if (msg->command[0] != 0 && msg->command[0] != 255) { |
| 399 | dprintf(INFO,"Recovery command: %d %s\n", |
| 400 | sizeof(msg->command), msg->command); |
| 401 | } |
| 402 | |
vijay kumar | 32e2e3c | 2014-08-01 21:10:28 +0530 | [diff] [blame] | 403 | if (!strcmp(msg->command, "boot-recovery")) { |
Stanimir Varbanov | be04199 | 2013-04-26 14:29:21 +0300 | [diff] [blame] | 404 | boot_into_recovery = 1; |
| 405 | } |
| 406 | |
Sundarajan Srinivasan | 151c04d | 2014-07-17 15:52:12 -0700 | [diff] [blame] | 407 | if (!strcmp("update-radio",msg->command)) |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 408 | { |
| 409 | /* 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] | 410 | int ret = get_boot_info_apps(UPDATE_STATUS, (unsigned int *) &update_status); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 411 | |
| 412 | if(!ret && (update_status & 0x01)) |
| 413 | { |
| 414 | dprintf(INFO,"radio update success\n"); |
Sundarajan Srinivasan | 151c04d | 2014-07-17 15:52:12 -0700 | [diff] [blame] | 415 | strlcpy(msg->status, "OKAY", sizeof(msg->status)); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 416 | } |
| 417 | else |
| 418 | { |
| 419 | dprintf(INFO,"radio update failed\n"); |
Sundarajan Srinivasan | 151c04d | 2014-07-17 15:52:12 -0700 | [diff] [blame] | 420 | strlcpy(msg->status, "failed-update", sizeof(msg->status)); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 421 | } |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 422 | boot_into_recovery = 1; // Boot in recovery mode |
| 423 | } |
Sundarajan Srinivasan | 151c04d | 2014-07-17 15:52:12 -0700 | [diff] [blame] | 424 | if (!strcmp("reset-device-info",msg->command)) |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 425 | { |
| 426 | reset_device_info(); |
| 427 | } |
Sundarajan Srinivasan | 151c04d | 2014-07-17 15:52:12 -0700 | [diff] [blame] | 428 | if (!strcmp("root-detect",msg->command)) |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 429 | { |
| 430 | set_device_root(); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 431 | } |
| 432 | else |
Sundarajan Srinivasan | 151c04d | 2014-07-17 15:52:12 -0700 | [diff] [blame] | 433 | goto out;// do nothing |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 434 | |
Sundarajan Srinivasan | 151c04d | 2014-07-17 15:52:12 -0700 | [diff] [blame] | 435 | strlcpy(msg->command, "", sizeof(msg->command)); // clearing recovery command |
| 436 | emmc_set_recovery_msg(msg); // send recovery message |
| 437 | |
| 438 | out: |
| 439 | if(msg) |
| 440 | free(msg); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 441 | return 0; |
| 442 | } |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 443 | |
| 444 | static int read_misc(unsigned page_offset, void *buf, unsigned size) |
| 445 | { |
| 446 | const char *ptn_name = "misc"; |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 447 | uint32_t pagesize = get_page_size(); |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 448 | unsigned offset; |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 449 | |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 450 | if (size == 0 || buf == NULL) |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 451 | return -1; |
| 452 | |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 453 | offset = page_offset * pagesize; |
| 454 | |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 455 | if (target_is_emmc_boot()) |
| 456 | { |
| 457 | int index; |
| 458 | unsigned long long ptn; |
| 459 | unsigned long long ptn_size; |
| 460 | |
| 461 | index = partition_get_index(ptn_name); |
| 462 | if (index == INVALID_PTN) |
| 463 | { |
| 464 | dprintf(CRITICAL, "No '%s' partition found\n", ptn_name); |
| 465 | return -1; |
| 466 | } |
| 467 | |
| 468 | ptn = partition_get_offset(index); |
| 469 | ptn_size = partition_get_size(index); |
| 470 | |
Channagoud Kadabi | 5d0371c | 2014-10-21 22:27:07 -0700 | [diff] [blame] | 471 | mmc_set_lun(partition_get_lun(index)); |
| 472 | |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 473 | if (ptn_size < offset + size) |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 474 | { |
| 475 | dprintf(CRITICAL, "Read request out of '%s' boundaries\n", |
| 476 | ptn_name); |
| 477 | return -1; |
| 478 | } |
| 479 | |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 480 | if (mmc_read(ptn + offset, (unsigned int *)buf, size)) |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 481 | { |
| 482 | dprintf(CRITICAL, "Reading MMC failed\n"); |
| 483 | return -1; |
| 484 | } |
| 485 | } |
| 486 | else |
| 487 | { |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 488 | dprintf(CRITICAL, "Misc partition not supported for NAND targets.\n"); |
| 489 | return -1; |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 490 | } |
| 491 | |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 492 | return 0; |
| 493 | } |
| 494 | |
Pavel Nedev | c728bb3 | 2013-04-05 16:58:05 +0300 | [diff] [blame] | 495 | int write_misc(unsigned page_offset, void *buf, unsigned size) |
| 496 | { |
| 497 | const char *ptn_name = "misc"; |
| 498 | void *scratch_addr = target_get_scratch_address(); |
| 499 | unsigned offset; |
| 500 | unsigned aligned_size; |
| 501 | |
| 502 | if (size == 0 || buf == NULL || scratch_addr == NULL) |
| 503 | return -1; |
| 504 | |
| 505 | if (target_is_emmc_boot()) |
| 506 | { |
| 507 | int index; |
| 508 | unsigned long long ptn; |
| 509 | unsigned long long ptn_size; |
| 510 | |
| 511 | index = partition_get_index(ptn_name); |
| 512 | if (index == INVALID_PTN) |
| 513 | { |
| 514 | dprintf(CRITICAL, "No '%s' partition found\n", ptn_name); |
| 515 | return -1; |
| 516 | } |
| 517 | |
| 518 | ptn = partition_get_offset(index); |
| 519 | ptn_size = partition_get_size(index); |
| 520 | |
| 521 | offset = page_offset * BLOCK_SIZE; |
| 522 | aligned_size = ROUND_TO_PAGE(size, (unsigned)BLOCK_SIZE - 1); |
| 523 | if (ptn_size < offset + aligned_size) |
| 524 | { |
| 525 | dprintf(CRITICAL, "Write request out of '%s' boundaries\n", |
| 526 | ptn_name); |
| 527 | return -1; |
| 528 | } |
| 529 | |
Mayank Grover | 467bf49 | 2017-10-24 12:27:53 +0530 | [diff] [blame] | 530 | /* This will ensure, we zeored out any extra bytes |
| 531 | we will push to emmc, to prevent information leak */ |
| 532 | if (aligned_size > size) |
| 533 | memset((scratch_addr + size), 0, (aligned_size-size)); |
| 534 | |
Pavel Nedev | c728bb3 | 2013-04-05 16:58:05 +0300 | [diff] [blame] | 535 | if (scratch_addr != buf) |
| 536 | memcpy(scratch_addr, buf, size); |
Channagoud Kadabi | 2776b65 | 2015-08-28 15:41:26 -0700 | [diff] [blame] | 537 | |
| 538 | /* Set Lun for misc partition */ |
| 539 | mmc_set_lun(partition_get_lun(index)); |
| 540 | |
Pavel Nedev | c728bb3 | 2013-04-05 16:58:05 +0300 | [diff] [blame] | 541 | if (mmc_write(ptn + offset, aligned_size, (unsigned int *)scratch_addr)) |
| 542 | { |
| 543 | dprintf(CRITICAL, "Writing MMC failed\n"); |
| 544 | return -1; |
| 545 | } |
| 546 | } |
| 547 | else |
| 548 | { |
| 549 | struct ptentry *ptn; |
| 550 | struct ptable *ptable; |
| 551 | unsigned pagesize = flash_page_size(); |
| 552 | |
| 553 | ptable = flash_get_ptable(); |
| 554 | if (ptable == NULL) |
| 555 | { |
| 556 | dprintf(CRITICAL, "Partition table not found\n"); |
| 557 | return -1; |
| 558 | } |
| 559 | |
| 560 | ptn = ptable_find(ptable, ptn_name); |
| 561 | if (ptn == NULL) |
| 562 | { |
| 563 | dprintf(CRITICAL, "No '%s' partition found\n", ptn_name); |
| 564 | return -1; |
| 565 | } |
| 566 | |
| 567 | offset = page_offset * pagesize; |
| 568 | aligned_size = ROUND_TO_PAGE(size, pagesize - 1); |
| 569 | if (ptn->length < offset + aligned_size) |
| 570 | { |
| 571 | dprintf(CRITICAL, "Write request out of '%s' boundaries\n", |
| 572 | ptn_name); |
| 573 | return -1; |
| 574 | } |
| 575 | |
Mayank Grover | 467bf49 | 2017-10-24 12:27:53 +0530 | [diff] [blame] | 576 | /* This will ensure, we zeored out any extra bytes |
| 577 | we will push, to prevent information leak */ |
| 578 | if (aligned_size > size) |
| 579 | memset((scratch_addr + size), 0, (aligned_size-size)); |
| 580 | |
Pavel Nedev | c728bb3 | 2013-04-05 16:58:05 +0300 | [diff] [blame] | 581 | if (scratch_addr != buf) |
| 582 | memcpy(scratch_addr, buf, size); |
Mayank Grover | 467bf49 | 2017-10-24 12:27:53 +0530 | [diff] [blame] | 583 | |
Pavel Nedev | c728bb3 | 2013-04-05 16:58:05 +0300 | [diff] [blame] | 584 | if (flash_write(ptn, offset, scratch_addr, aligned_size)) { |
| 585 | dprintf(CRITICAL, "Writing flash failed\n"); |
| 586 | return -1; |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | return 0; |
| 591 | } |
| 592 | |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 593 | int get_ffbm(char *ffbm, unsigned size) |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 594 | { |
| 595 | const char *ffbm_cmd = "ffbm-"; |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 596 | uint32_t page_size = get_page_size(); |
| 597 | char *ffbm_page_buffer = NULL; |
| 598 | int retval = 0; |
| 599 | if (size < FFBM_MODE_BUF_SIZE || size >= page_size) |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 600 | { |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 601 | dprintf(CRITICAL, "Invalid size argument passed to get_ffbm\n"); |
| 602 | retval = -1; |
| 603 | goto cleanup; |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 604 | } |
Parth Dixit | 75b1681 | 2016-04-05 19:17:17 +0530 | [diff] [blame] | 605 | ffbm_page_buffer = (char*)memalign(CACHE_LINE, page_size); |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 606 | if (!ffbm_page_buffer) |
| 607 | { |
| 608 | dprintf(CRITICAL, "Failed to alloc buffer for ffbm cookie\n"); |
| 609 | retval = -1; |
| 610 | goto cleanup; |
| 611 | } |
| 612 | if (read_misc(0, ffbm_page_buffer, page_size)) |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 613 | { |
| 614 | dprintf(CRITICAL, "Error reading MISC partition\n"); |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 615 | retval = -1; |
| 616 | goto cleanup; |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 617 | } |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 618 | ffbm_page_buffer[size] = '\0'; |
| 619 | if (strncmp(ffbm_cmd, ffbm_page_buffer, strlen(ffbm_cmd))) |
| 620 | { |
| 621 | retval = 0; |
| 622 | goto cleanup; |
| 623 | } |
| 624 | else |
| 625 | { |
| 626 | if (strlcpy(ffbm, ffbm_page_buffer, size) < |
| 627 | FFBM_MODE_BUF_SIZE -1) |
| 628 | { |
| 629 | dprintf(CRITICAL, "Invalid string in misc partition\n"); |
| 630 | retval = -1; |
| 631 | } |
| 632 | else |
| 633 | retval = 1; |
| 634 | } |
| 635 | cleanup: |
| 636 | if(ffbm_page_buffer) |
| 637 | free(ffbm_page_buffer); |
| 638 | return retval; |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 639 | } |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 640 | |
| 641 | |