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; |
| 137 | unsigned int ssd_cookie[2] = {0x53534443, 0x4F4F4B49}; |
| 138 | unsigned pagesize = flash_page_size(); |
| 139 | unsigned pagemask = pagesize -1; |
| 140 | unsigned n = 0; |
| 141 | |
| 142 | ptable = flash_get_ptable(); |
| 143 | if (ptable == NULL) { |
| 144 | dprintf(CRITICAL, "ERROR: Partition table not found\n"); |
| 145 | return -1; |
| 146 | } |
| 147 | |
| 148 | n = (sizeof(ssd_cookie) + pagemask) & (~pagemask); |
| 149 | |
| 150 | ptn = ptable_find(ptable, name); |
| 151 | if (ptn == NULL) { |
| 152 | dprintf(CRITICAL, "ERROR: No %s partition found\n", name); |
| 153 | return -1; |
| 154 | } |
| 155 | |
| 156 | if (flash_write(ptn, 0, ssd_cookie, n)) { |
| 157 | dprintf(CRITICAL, "ERROR: flash write fail!\n"); |
| 158 | return -1; |
| 159 | } |
| 160 | |
| 161 | dprintf(INFO, "FOTA partition written successfully!"); |
| 162 | return 0; |
| 163 | } |
| 164 | |
| 165 | int get_boot_info_apps (char type, unsigned int *status) |
| 166 | { |
| 167 | boot_info_for_apps apps_boot_info; |
| 168 | int ret = 0; |
| 169 | |
| 170 | ret = smem_read_alloc_entry(SMEM_BOOT_INFO_FOR_APPS, |
| 171 | &apps_boot_info, sizeof(apps_boot_info)); |
| 172 | if (ret) |
| 173 | { |
| 174 | dprintf(CRITICAL, "ERROR: unable to read shared memory for apps boot info %d\n",ret); |
| 175 | return ret; |
| 176 | } |
| 177 | |
| 178 | dprintf(INFO,"boot flag %x update status %x\n",apps_boot_info.boot_flags, |
| 179 | apps_boot_info.status.update_status); |
| 180 | |
| 181 | if(type == BOOT_FLAGS) |
| 182 | *status = apps_boot_info.boot_flags; |
| 183 | else if(type == UPDATE_STATUS) |
| 184 | *status = apps_boot_info.status.update_status; |
| 185 | |
| 186 | return ret; |
| 187 | } |
| 188 | |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 189 | /* Bootloader / Recovery Flow |
| 190 | * |
| 191 | * On every boot, the bootloader will read the recovery_message |
| 192 | * from flash and check the command field. The bootloader should |
| 193 | * deal with the command field not having a 0 terminator correctly |
| 194 | * (so as to not crash if the block is invalid or corrupt). |
| 195 | * |
| 196 | * The bootloader will have to publish the partition that contains |
| 197 | * the recovery_message to the linux kernel so it can update it. |
| 198 | * |
| 199 | * if command == "boot-recovery" -> boot recovery.img |
| 200 | * else if command == "update-radio" -> update radio image (below) |
| 201 | * else -> boot boot.img (normal boot) |
| 202 | * |
| 203 | * Radio Update Flow |
| 204 | * 1. the bootloader will attempt to load and validate the header |
| 205 | * 2. if the header is invalid, status="invalid-update", goto #8 |
| 206 | * 3. display the busy image on-screen |
| 207 | * 4. if the update image is invalid, status="invalid-radio-image", goto #8 |
| 208 | * 5. attempt to update the firmware (depending on the command) |
| 209 | * 6. if successful, status="okay", goto #8 |
| 210 | * 7. if failed, and the old image can still boot, status="failed-update" |
| 211 | * 8. write the recovery_message, leaving the recovery field |
| 212 | * unchanged, updating status, and setting command to |
| 213 | * "boot-recovery" |
| 214 | * 9. reboot |
| 215 | * |
| 216 | * The bootloader will not modify or erase the cache partition. |
| 217 | * It is recovery's responsibility to clean up the mess afterwards. |
| 218 | */ |
| 219 | |
| 220 | int recovery_init (void) |
| 221 | { |
| 222 | struct recovery_message msg; |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 223 | char partition_name[32]; |
| 224 | unsigned valid_command = 0; |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 225 | int update_status = 0; |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 226 | |
| 227 | // get recovery message |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 228 | if (get_recovery_message(&msg)) |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 229 | return -1; |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 230 | msg.command[sizeof(msg.command)-1] = '\0'; //Ensure termination |
Pavel Nedev | 96a9aea | 2013-02-26 15:16:26 -0800 | [diff] [blame] | 231 | if (msg.command[0] != 0 && msg.command[0] != 255) { |
| 232 | dprintf(INFO,"Recovery command: %d %s\n", |
| 233 | sizeof(msg.command), msg.command); |
| 234 | } |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 235 | |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 236 | if (!strcmp("boot-recovery",msg.command)) |
| 237 | { |
| 238 | if(!strcmp("RADIO",msg.status)) |
| 239 | { |
| 240 | /* 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] | 241 | int ret = get_boot_info_apps(UPDATE_STATUS, (unsigned int *) &update_status); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 242 | |
| 243 | if(!ret && (update_status & 0x01)) |
| 244 | { |
| 245 | dprintf(INFO,"radio update success\n"); |
Ajay Dudani | f63d02f | 2011-10-01 08:29:53 -0700 | [diff] [blame] | 246 | strlcpy(msg.status, "OKAY", sizeof(msg.status)); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 247 | } |
| 248 | else |
| 249 | { |
| 250 | dprintf(INFO,"radio update failed\n"); |
Ajay Dudani | f63d02f | 2011-10-01 08:29:53 -0700 | [diff] [blame] | 251 | strlcpy(msg.status, "failed-update", sizeof(msg.status)); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 252 | } |
Ajay Dudani | f63d02f | 2011-10-01 08:29:53 -0700 | [diff] [blame] | 253 | strlcpy(msg.command, "", sizeof(msg.command)); // clearing recovery command |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 254 | set_recovery_message(&msg); // send recovery message |
| 255 | boot_into_recovery = 1; // Boot in recovery mode |
| 256 | return 0; |
| 257 | } |
| 258 | |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 259 | valid_command = 1; |
Ajay Dudani | f63d02f | 2011-10-01 08:29:53 -0700 | [diff] [blame] | 260 | strlcpy(msg.command, "", sizeof(msg.command)); // to safe against multiple reboot into recovery |
| 261 | strlcpy(msg.status, "OKAY", sizeof(msg.status)); |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 262 | set_recovery_message(&msg); // send recovery message |
| 263 | boot_into_recovery = 1; // Boot in recovery mode |
| 264 | return 0; |
| 265 | } |
| 266 | |
| 267 | if (!strcmp("update-radio",msg.command)) { |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 268 | dprintf(INFO,"start radio update\n"); |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 269 | valid_command = 1; |
Ajay Dudani | f63d02f | 2011-10-01 08:29:53 -0700 | [diff] [blame] | 270 | strlcpy(partition_name, "FOTA", sizeof(partition_name)); |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | //Todo: Add support for bootloader update too. |
| 274 | |
| 275 | if(!valid_command) { |
| 276 | //We need not to do anything |
| 277 | return 0; // Boot in normal mode |
| 278 | } |
| 279 | |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 280 | if (set_ssd_radio_update(partition_name)) { |
| 281 | /* If writing to FOTA partition fails */ |
Ajay Dudani | f63d02f | 2011-10-01 08:29:53 -0700 | [diff] [blame] | 282 | strlcpy(msg.command, "", sizeof(msg.command)); |
| 283 | strlcpy(msg.status, "failed-update", sizeof(msg.status)); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 284 | goto SEND_RECOVERY_MSG; |
| 285 | } |
| 286 | else { |
| 287 | /* Setting this to check the radio update status */ |
Ajay Dudani | f63d02f | 2011-10-01 08:29:53 -0700 | [diff] [blame] | 288 | strlcpy(msg.command, "boot-recovery", sizeof(msg.command)); |
Ajay Dudani | f4caa94 | 2011-10-02 08:57:15 -0700 | [diff] [blame] | 289 | strlcpy(msg.status, "RADIO", sizeof(msg.status)); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 290 | goto SEND_RECOVERY_MSG; |
| 291 | } |
Ajay Dudani | f63d02f | 2011-10-01 08:29:53 -0700 | [diff] [blame] | 292 | strlcpy(msg.status, "OKAY", sizeof(msg.status)); |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 293 | |
| 294 | SEND_RECOVERY_MSG: |
Shashank Mittal | 024c033 | 2010-02-03 11:44:00 -0800 | [diff] [blame] | 295 | set_recovery_message(&msg); // send recovery message |
| 296 | boot_into_recovery = 1; // Boot in recovery mode |
| 297 | reboot_device(0); |
| 298 | return 0; |
| 299 | } |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 300 | |
| 301 | static int emmc_set_recovery_msg(struct recovery_message *out) |
| 302 | { |
| 303 | char *ptn_name = "misc"; |
| 304 | unsigned long long ptn = 0; |
Mayank Grover | 9db9724 | 2017-06-16 12:41:56 +0530 | [diff] [blame^] | 305 | unsigned blocksize = mmc_get_device_blocksize(); |
| 306 | unsigned int size = ROUND_TO_PAGE(sizeof(*out), (unsigned)blocksize - 1); |
| 307 | unsigned char *data = NULL; |
| 308 | int ret = 0; |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 309 | int index = INVALID_PTN; |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 310 | |
Mayank Grover | 9db9724 | 2017-06-16 12:41:56 +0530 | [diff] [blame^] | 311 | data = malloc(size); |
| 312 | if(!data) |
| 313 | { |
| 314 | dprintf(CRITICAL,"memory allocation error \n"); |
| 315 | ret = -1; |
| 316 | goto out; |
| 317 | } |
| 318 | |
Unnati Gandhi | 0c8e7c5 | 2014-07-17 14:33:09 +0530 | [diff] [blame] | 319 | index = partition_get_index((const char *) ptn_name); |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 320 | ptn = partition_get_offset(index); |
Channagoud Kadabi | 5d0371c | 2014-10-21 22:27:07 -0700 | [diff] [blame] | 321 | mmc_set_lun(partition_get_lun(index)); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 322 | if(ptn == 0) { |
| 323 | dprintf(CRITICAL,"partition %s doesn't exist\n",ptn_name); |
Mayank Grover | 9db9724 | 2017-06-16 12:41:56 +0530 | [diff] [blame^] | 324 | ret = -1; |
| 325 | goto out; |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 326 | } |
Mayank Grover | 9db9724 | 2017-06-16 12:41:56 +0530 | [diff] [blame^] | 327 | memset(data, 0, size); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 328 | memcpy(data, out, sizeof(*out)); |
| 329 | if (mmc_write(ptn , size, (unsigned int*)data)) { |
| 330 | dprintf(CRITICAL,"mmc write failure %s %d\n",ptn_name, sizeof(*out)); |
Mayank Grover | 9db9724 | 2017-06-16 12:41:56 +0530 | [diff] [blame^] | 331 | ret = -1; |
| 332 | goto out; |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 333 | } |
Mayank Grover | 9db9724 | 2017-06-16 12:41:56 +0530 | [diff] [blame^] | 334 | out: |
| 335 | if (data) |
| 336 | free(data); |
| 337 | return ret; |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | static int emmc_get_recovery_msg(struct recovery_message *in) |
| 341 | { |
| 342 | char *ptn_name = "misc"; |
| 343 | unsigned long long ptn = 0; |
Sundarajan Srinivasan | 151c04d | 2014-07-17 15:52:12 -0700 | [diff] [blame] | 344 | unsigned int size; |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 345 | int index = INVALID_PTN; |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 346 | |
Sundarajan Srinivasan | 151c04d | 2014-07-17 15:52:12 -0700 | [diff] [blame] | 347 | size = mmc_get_device_blocksize(); |
Unnati Gandhi | 0c8e7c5 | 2014-07-17 14:33:09 +0530 | [diff] [blame] | 348 | index = partition_get_index((const char *) ptn_name); |
Channagoud Kadabi | f280563 | 2015-01-07 16:46:50 -0800 | [diff] [blame] | 349 | if (index < 0) |
| 350 | { |
| 351 | dprintf(CRITICAL, "%s: Partition not found\n", ptn_name); |
| 352 | return -1; |
| 353 | } |
| 354 | |
Kinson Chik | f1a4351 | 2011-07-14 11:28:39 -0700 | [diff] [blame] | 355 | ptn = partition_get_offset(index); |
Channagoud Kadabi | 5d0371c | 2014-10-21 22:27:07 -0700 | [diff] [blame] | 356 | mmc_set_lun(partition_get_lun(index)); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 357 | if(ptn == 0) { |
| 358 | dprintf(CRITICAL,"partition %s doesn't exist\n",ptn_name); |
| 359 | return -1; |
| 360 | } |
Sundarajan Srinivasan | 151c04d | 2014-07-17 15:52:12 -0700 | [diff] [blame] | 361 | if (mmc_read(ptn , (unsigned int*)in, size)) { |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 362 | dprintf(CRITICAL,"mmc read failure %s %d\n",ptn_name, size); |
| 363 | return -1; |
| 364 | } |
Sundarajan Srinivasan | 151c04d | 2014-07-17 15:52:12 -0700 | [diff] [blame] | 365 | |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 366 | return 0; |
| 367 | } |
| 368 | |
| 369 | int _emmc_recovery_init(void) |
| 370 | { |
| 371 | int update_status = 0; |
Sundarajan Srinivasan | 151c04d | 2014-07-17 15:52:12 -0700 | [diff] [blame] | 372 | struct recovery_message *msg; |
| 373 | uint32_t block_size = 0; |
| 374 | |
| 375 | block_size = mmc_get_device_blocksize(); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 376 | |
| 377 | // get recovery message |
Sundarajan Srinivasan | 151c04d | 2014-07-17 15:52:12 -0700 | [diff] [blame] | 378 | msg = (struct recovery_message *)memalign(CACHE_LINE, block_size); |
| 379 | ASSERT(msg); |
| 380 | |
| 381 | if(emmc_get_recovery_msg(msg)) |
| 382 | { |
| 383 | if(msg) |
| 384 | free(msg); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 385 | return -1; |
Pavel Nedev | 96a9aea | 2013-02-26 15:16:26 -0800 | [diff] [blame] | 386 | } |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 387 | |
Sundarajan Srinivasan | 151c04d | 2014-07-17 15:52:12 -0700 | [diff] [blame] | 388 | msg->command[sizeof(msg->command)-1] = '\0'; //Ensure termination |
| 389 | if (msg->command[0] != 0 && msg->command[0] != 255) { |
| 390 | dprintf(INFO,"Recovery command: %d %s\n", |
| 391 | sizeof(msg->command), msg->command); |
| 392 | } |
| 393 | |
vijay kumar | 32e2e3c | 2014-08-01 21:10:28 +0530 | [diff] [blame] | 394 | if (!strcmp(msg->command, "boot-recovery")) { |
Stanimir Varbanov | be04199 | 2013-04-26 14:29:21 +0300 | [diff] [blame] | 395 | boot_into_recovery = 1; |
| 396 | } |
| 397 | |
Sundarajan Srinivasan | 151c04d | 2014-07-17 15:52:12 -0700 | [diff] [blame] | 398 | if (!strcmp("update-radio",msg->command)) |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 399 | { |
| 400 | /* 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] | 401 | int ret = get_boot_info_apps(UPDATE_STATUS, (unsigned int *) &update_status); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 402 | |
| 403 | if(!ret && (update_status & 0x01)) |
| 404 | { |
| 405 | dprintf(INFO,"radio update success\n"); |
Sundarajan Srinivasan | 151c04d | 2014-07-17 15:52:12 -0700 | [diff] [blame] | 406 | strlcpy(msg->status, "OKAY", sizeof(msg->status)); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 407 | } |
| 408 | else |
| 409 | { |
| 410 | dprintf(INFO,"radio update failed\n"); |
Sundarajan Srinivasan | 151c04d | 2014-07-17 15:52:12 -0700 | [diff] [blame] | 411 | strlcpy(msg->status, "failed-update", sizeof(msg->status)); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 412 | } |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 413 | boot_into_recovery = 1; // Boot in recovery mode |
| 414 | } |
Sundarajan Srinivasan | 151c04d | 2014-07-17 15:52:12 -0700 | [diff] [blame] | 415 | if (!strcmp("reset-device-info",msg->command)) |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 416 | { |
| 417 | reset_device_info(); |
| 418 | } |
Sundarajan Srinivasan | 151c04d | 2014-07-17 15:52:12 -0700 | [diff] [blame] | 419 | if (!strcmp("root-detect",msg->command)) |
Shashank Mittal | 162244e | 2011-08-08 19:01:25 -0700 | [diff] [blame] | 420 | { |
| 421 | set_device_root(); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 422 | } |
| 423 | else |
Sundarajan Srinivasan | 151c04d | 2014-07-17 15:52:12 -0700 | [diff] [blame] | 424 | goto out;// do nothing |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 425 | |
Sundarajan Srinivasan | 151c04d | 2014-07-17 15:52:12 -0700 | [diff] [blame] | 426 | strlcpy(msg->command, "", sizeof(msg->command)); // clearing recovery command |
| 427 | emmc_set_recovery_msg(msg); // send recovery message |
| 428 | |
| 429 | out: |
| 430 | if(msg) |
| 431 | free(msg); |
Subbaraman Narayanamurthy | 0e445b0 | 2011-06-19 21:34:46 -0700 | [diff] [blame] | 432 | return 0; |
| 433 | } |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 434 | |
| 435 | static int read_misc(unsigned page_offset, void *buf, unsigned size) |
| 436 | { |
| 437 | const char *ptn_name = "misc"; |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 438 | uint32_t pagesize = get_page_size(); |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 439 | unsigned offset; |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 440 | |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 441 | if (size == 0 || buf == NULL) |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 442 | return -1; |
| 443 | |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 444 | offset = page_offset * pagesize; |
| 445 | |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 446 | if (target_is_emmc_boot()) |
| 447 | { |
| 448 | int index; |
| 449 | unsigned long long ptn; |
| 450 | unsigned long long ptn_size; |
| 451 | |
| 452 | index = partition_get_index(ptn_name); |
| 453 | if (index == INVALID_PTN) |
| 454 | { |
| 455 | dprintf(CRITICAL, "No '%s' partition found\n", ptn_name); |
| 456 | return -1; |
| 457 | } |
| 458 | |
| 459 | ptn = partition_get_offset(index); |
| 460 | ptn_size = partition_get_size(index); |
| 461 | |
Channagoud Kadabi | 5d0371c | 2014-10-21 22:27:07 -0700 | [diff] [blame] | 462 | mmc_set_lun(partition_get_lun(index)); |
| 463 | |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 464 | if (ptn_size < offset + size) |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 465 | { |
| 466 | dprintf(CRITICAL, "Read request out of '%s' boundaries\n", |
| 467 | ptn_name); |
| 468 | return -1; |
| 469 | } |
| 470 | |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 471 | if (mmc_read(ptn + offset, (unsigned int *)buf, size)) |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 472 | { |
| 473 | dprintf(CRITICAL, "Reading MMC failed\n"); |
| 474 | return -1; |
| 475 | } |
| 476 | } |
| 477 | else |
| 478 | { |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 479 | dprintf(CRITICAL, "Misc partition not supported for NAND targets.\n"); |
| 480 | return -1; |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 481 | } |
| 482 | |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 483 | return 0; |
| 484 | } |
| 485 | |
Pavel Nedev | c728bb3 | 2013-04-05 16:58:05 +0300 | [diff] [blame] | 486 | int write_misc(unsigned page_offset, void *buf, unsigned size) |
| 487 | { |
| 488 | const char *ptn_name = "misc"; |
| 489 | void *scratch_addr = target_get_scratch_address(); |
| 490 | unsigned offset; |
| 491 | unsigned aligned_size; |
| 492 | |
| 493 | if (size == 0 || buf == NULL || scratch_addr == NULL) |
| 494 | return -1; |
| 495 | |
| 496 | if (target_is_emmc_boot()) |
| 497 | { |
| 498 | int index; |
| 499 | unsigned long long ptn; |
| 500 | unsigned long long ptn_size; |
| 501 | |
| 502 | index = partition_get_index(ptn_name); |
| 503 | if (index == INVALID_PTN) |
| 504 | { |
| 505 | dprintf(CRITICAL, "No '%s' partition found\n", ptn_name); |
| 506 | return -1; |
| 507 | } |
| 508 | |
| 509 | ptn = partition_get_offset(index); |
| 510 | ptn_size = partition_get_size(index); |
| 511 | |
| 512 | offset = page_offset * BLOCK_SIZE; |
| 513 | aligned_size = ROUND_TO_PAGE(size, (unsigned)BLOCK_SIZE - 1); |
| 514 | if (ptn_size < offset + aligned_size) |
| 515 | { |
| 516 | dprintf(CRITICAL, "Write request out of '%s' boundaries\n", |
| 517 | ptn_name); |
| 518 | return -1; |
| 519 | } |
| 520 | |
| 521 | if (scratch_addr != buf) |
| 522 | memcpy(scratch_addr, buf, size); |
Channagoud Kadabi | 2776b65 | 2015-08-28 15:41:26 -0700 | [diff] [blame] | 523 | |
| 524 | /* Set Lun for misc partition */ |
| 525 | mmc_set_lun(partition_get_lun(index)); |
| 526 | |
Pavel Nedev | c728bb3 | 2013-04-05 16:58:05 +0300 | [diff] [blame] | 527 | if (mmc_write(ptn + offset, aligned_size, (unsigned int *)scratch_addr)) |
| 528 | { |
| 529 | dprintf(CRITICAL, "Writing MMC failed\n"); |
| 530 | return -1; |
| 531 | } |
| 532 | } |
| 533 | else |
| 534 | { |
| 535 | struct ptentry *ptn; |
| 536 | struct ptable *ptable; |
| 537 | unsigned pagesize = flash_page_size(); |
| 538 | |
| 539 | ptable = flash_get_ptable(); |
| 540 | if (ptable == NULL) |
| 541 | { |
| 542 | dprintf(CRITICAL, "Partition table not found\n"); |
| 543 | return -1; |
| 544 | } |
| 545 | |
| 546 | ptn = ptable_find(ptable, ptn_name); |
| 547 | if (ptn == NULL) |
| 548 | { |
| 549 | dprintf(CRITICAL, "No '%s' partition found\n", ptn_name); |
| 550 | return -1; |
| 551 | } |
| 552 | |
| 553 | offset = page_offset * pagesize; |
| 554 | aligned_size = ROUND_TO_PAGE(size, pagesize - 1); |
| 555 | if (ptn->length < offset + aligned_size) |
| 556 | { |
| 557 | dprintf(CRITICAL, "Write request out of '%s' boundaries\n", |
| 558 | ptn_name); |
| 559 | return -1; |
| 560 | } |
| 561 | |
| 562 | if (scratch_addr != buf) |
| 563 | memcpy(scratch_addr, buf, size); |
| 564 | if (flash_write(ptn, offset, scratch_addr, aligned_size)) { |
| 565 | dprintf(CRITICAL, "Writing flash failed\n"); |
| 566 | return -1; |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | return 0; |
| 571 | } |
| 572 | |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 573 | int get_ffbm(char *ffbm, unsigned size) |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 574 | { |
| 575 | const char *ffbm_cmd = "ffbm-"; |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 576 | uint32_t page_size = get_page_size(); |
| 577 | char *ffbm_page_buffer = NULL; |
| 578 | int retval = 0; |
| 579 | if (size < FFBM_MODE_BUF_SIZE || size >= page_size) |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 580 | { |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 581 | dprintf(CRITICAL, "Invalid size argument passed to get_ffbm\n"); |
| 582 | retval = -1; |
| 583 | goto cleanup; |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 584 | } |
Parth Dixit | 75b1681 | 2016-04-05 19:17:17 +0530 | [diff] [blame] | 585 | ffbm_page_buffer = (char*)memalign(CACHE_LINE, page_size); |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 586 | if (!ffbm_page_buffer) |
| 587 | { |
| 588 | dprintf(CRITICAL, "Failed to alloc buffer for ffbm cookie\n"); |
| 589 | retval = -1; |
| 590 | goto cleanup; |
| 591 | } |
| 592 | if (read_misc(0, ffbm_page_buffer, page_size)) |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 593 | { |
| 594 | dprintf(CRITICAL, "Error reading MISC partition\n"); |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 595 | retval = -1; |
| 596 | goto cleanup; |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 597 | } |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 598 | ffbm_page_buffer[size] = '\0'; |
| 599 | if (strncmp(ffbm_cmd, ffbm_page_buffer, strlen(ffbm_cmd))) |
| 600 | { |
| 601 | retval = 0; |
| 602 | goto cleanup; |
| 603 | } |
| 604 | else |
| 605 | { |
| 606 | if (strlcpy(ffbm, ffbm_page_buffer, size) < |
| 607 | FFBM_MODE_BUF_SIZE -1) |
| 608 | { |
| 609 | dprintf(CRITICAL, "Invalid string in misc partition\n"); |
| 610 | retval = -1; |
| 611 | } |
| 612 | else |
| 613 | retval = 1; |
| 614 | } |
| 615 | cleanup: |
| 616 | if(ffbm_page_buffer) |
| 617 | free(ffbm_page_buffer); |
| 618 | return retval; |
Pavel Nedev | a1e6232 | 2013-04-05 15:21:36 +0300 | [diff] [blame] | 619 | } |
Deepa Dinamani | 41fa8d6 | 2013-05-23 13:25:36 -0700 | [diff] [blame] | 620 | |
| 621 | |