blob: a4048ab2f05be39f2bb52c9e942579892b473fd1 [file] [log] [blame]
Kishor PK8a2783f2017-04-20 14:51:04 +05301/* Copyright (c) 2010-2017, The Linux Foundation. All rights reserved.
Shashank Mittal024c0332010-02-03 11:44:00 -08002
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 Truongf3ac7b32013-02-13 01:07:28 -080012 * * Neither the name of The Linux Foundation nor the names of its
Shashank Mittal024c0332010-02-03 11:44:00 -080013 * 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 Srinivasan151c04d2014-07-17 15:52:12 -070035#include <arch/defines.h>
36#include <malloc.h>
Shashank Mittal024c0332010-02-03 11:44:00 -080037
38#include <dev/flash.h>
39#include <lib/ptable.h>
40#include <dev/keys.h>
Greg Griscod6250552011-06-29 14:40:23 -070041#include <platform.h>
Pavel Nedeva1e62322013-04-05 15:21:36 +030042#include <target.h>
Kinson Chikf1a43512011-07-14 11:28:39 -070043#include <partition_parser.h>
Greg Griscod2471ef2011-07-14 13:00:42 -070044#include <mmc.h>
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +053045#include <malloc.h>
Mayank Grover7e40ad22017-11-13 17:17:53 +053046#include <stdlib.h>
Shashank Mittal024c0332010-02-03 11:44:00 -080047#include "recovery.h"
48#include "bootimg.h"
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -070049#include "smem.h"
50
51#define BOOT_FLAGS 1
52#define UPDATE_STATUS 2
Shashank Mittal024c0332010-02-03 11:44:00 -080053
54static const int MISC_PAGES = 3; // number of pages to save
55static const int MISC_COMMAND_PAGE = 1; // bootloader command is this page
56static char buf[4096];
Pavel Nedeva1e62322013-04-05 15:21:36 +030057
Shashank Mittal024c0332010-02-03 11:44:00 -080058unsigned boot_into_recovery = 0;
59
Deepa Dinamani41fa8d62013-05-23 13:25:36 -070060extern uint32_t get_page_size();
Shashank Mittal162244e2011-08-08 19:01:25 -070061extern void reset_device_info();
62extern void set_device_root();
Shashank Mittal024c0332010-02-03 11:44:00 -080063
64int 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 Griscod6250552011-06-29 14:40:23 -070085 if (flash_read(ptn, offset, (void *) buf, pagesize)) {
Shashank Mittal024c0332010-02-03 11:44:00 -080086 dprintf(CRITICAL, "ERROR: Cannot read recovery_header\n");
87 return -1;
88 }
89 memcpy(out, buf, sizeof(*out));
90 return 0;
91}
92
93int 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 Nedev5d4a7052013-05-13 15:14:45 +0300100 void *scratch_addr = target_get_scratch_address();
Shashank Mittal024c0332010-02-03 11:44:00 -0800101
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 Nedev5d4a7052013-05-13 15:14:45 +0300117 if (flash_read(ptn, offset, scratch_addr, n)) {
Shashank Mittal024c0332010-02-03 11:44:00 -0800118 dprintf(CRITICAL, "ERROR: Cannot read recovery_header\n");
119 return -1;
120 }
121
122 offset += (pagesize * MISC_COMMAND_PAGE);
Pavel Nedev5d4a7052013-05-13 15:14:45 +0300123 offset += (unsigned) scratch_addr;
Greg Griscod6250552011-06-29 14:40:23 -0700124 memcpy((void *) offset, in, sizeof(*in));
Pavel Nedev5d4a7052013-05-13 15:14:45 +0300125 if (flash_write(ptn, 0, scratch_addr, n)) {
Shashank Mittal024c0332010-02-03 11:44:00 -0800126 dprintf(CRITICAL, "ERROR: flash write fail!\n");
127 return -1;
128 }
Greg Griscod6250552011-06-29 14:40:23 -0700129 return 0;
Shashank Mittal024c0332010-02-03 11:44:00 -0800130}
131
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700132static int set_ssd_radio_update (char *name)
133{
134 struct ptentry *ptn;
135 struct ptable *ptable;
Mayank Groverba853de2017-06-15 16:20:18 +0530136 unsigned int *ssd_cookie;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700137 unsigned pagesize = flash_page_size();
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700138
139 ptable = flash_get_ptable();
140 if (ptable == NULL) {
141 dprintf(CRITICAL, "ERROR: Partition table not found\n");
142 return -1;
143 }
144
Mayank Groverba853de2017-06-15 16:20:18 +0530145 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 Narayanamurthy0e445b02011-06-19 21:34:46 -0700153
154 ptn = ptable_find(ptable, name);
155 if (ptn == NULL) {
156 dprintf(CRITICAL, "ERROR: No %s partition found\n", name);
Mayank Groverba853de2017-06-15 16:20:18 +0530157 goto out;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700158 }
159
Mayank Groverba853de2017-06-15 16:20:18 +0530160 if (flash_write(ptn, 0, ssd_cookie, pagesize)) {
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700161 dprintf(CRITICAL, "ERROR: flash write fail!\n");
Mayank Groverba853de2017-06-15 16:20:18 +0530162 goto out;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700163 }
164
Mayank Groverba853de2017-06-15 16:20:18 +0530165 free(ssd_cookie);
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700166 dprintf(INFO, "FOTA partition written successfully!");
167 return 0;
Mayank Groverba853de2017-06-15 16:20:18 +0530168out:
169 free(ssd_cookie);
170 return -1;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700171}
172
173int 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 Mittal024c0332010-02-03 11:44:00 -0800197/* 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
228int recovery_init (void)
229{
230 struct recovery_message msg;
Shashank Mittal024c0332010-02-03 11:44:00 -0800231 char partition_name[32];
232 unsigned valid_command = 0;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700233 int update_status = 0;
Shashank Mittal024c0332010-02-03 11:44:00 -0800234
235 // get recovery message
Greg Griscod6250552011-06-29 14:40:23 -0700236 if (get_recovery_message(&msg))
Shashank Mittal024c0332010-02-03 11:44:00 -0800237 return -1;
Shashank Mittal024c0332010-02-03 11:44:00 -0800238 msg.command[sizeof(msg.command)-1] = '\0'; //Ensure termination
Pavel Nedev96a9aea2013-02-26 15:16:26 -0800239 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 Mittal024c0332010-02-03 11:44:00 -0800243
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700244 if (!strcmp("boot-recovery",msg.command))
245 {
246 if(!strcmp("RADIO",msg.status))
247 {
248 /* We're now here due to radio update, so check for update status */
Greg Griscod2471ef2011-07-14 13:00:42 -0700249 int ret = get_boot_info_apps(UPDATE_STATUS, (unsigned int *) &update_status);
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700250
251 if(!ret && (update_status & 0x01))
252 {
253 dprintf(INFO,"radio update success\n");
Ajay Dudanif63d02f2011-10-01 08:29:53 -0700254 strlcpy(msg.status, "OKAY", sizeof(msg.status));
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700255 }
256 else
257 {
258 dprintf(INFO,"radio update failed\n");
Ajay Dudanif63d02f2011-10-01 08:29:53 -0700259 strlcpy(msg.status, "failed-update", sizeof(msg.status));
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700260 }
Ajay Dudanif63d02f2011-10-01 08:29:53 -0700261 strlcpy(msg.command, "", sizeof(msg.command)); // clearing recovery command
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700262 set_recovery_message(&msg); // send recovery message
263 boot_into_recovery = 1; // Boot in recovery mode
264 return 0;
265 }
266
Shashank Mittal024c0332010-02-03 11:44:00 -0800267 valid_command = 1;
Ajay Dudanif63d02f2011-10-01 08:29:53 -0700268 strlcpy(msg.command, "", sizeof(msg.command)); // to safe against multiple reboot into recovery
269 strlcpy(msg.status, "OKAY", sizeof(msg.status));
Shashank Mittal024c0332010-02-03 11:44:00 -0800270 set_recovery_message(&msg); // send recovery message
271 boot_into_recovery = 1; // Boot in recovery mode
272 return 0;
273 }
274
275 if (!strcmp("update-radio",msg.command)) {
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700276 dprintf(INFO,"start radio update\n");
Shashank Mittal024c0332010-02-03 11:44:00 -0800277 valid_command = 1;
Ajay Dudanif63d02f2011-10-01 08:29:53 -0700278 strlcpy(partition_name, "FOTA", sizeof(partition_name));
Shashank Mittal024c0332010-02-03 11:44:00 -0800279 }
280
281 //Todo: Add support for bootloader update too.
282
283 if(!valid_command) {
284 //We need not to do anything
285 return 0; // Boot in normal mode
286 }
287
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700288 if (set_ssd_radio_update(partition_name)) {
289 /* If writing to FOTA partition fails */
Ajay Dudanif63d02f2011-10-01 08:29:53 -0700290 strlcpy(msg.command, "", sizeof(msg.command));
291 strlcpy(msg.status, "failed-update", sizeof(msg.status));
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700292 goto SEND_RECOVERY_MSG;
293 }
294 else {
295 /* Setting this to check the radio update status */
Ajay Dudanif63d02f2011-10-01 08:29:53 -0700296 strlcpy(msg.command, "boot-recovery", sizeof(msg.command));
Ajay Dudanif4caa942011-10-02 08:57:15 -0700297 strlcpy(msg.status, "RADIO", sizeof(msg.status));
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700298 goto SEND_RECOVERY_MSG;
299 }
Ajay Dudanif63d02f2011-10-01 08:29:53 -0700300 strlcpy(msg.status, "OKAY", sizeof(msg.status));
Shashank Mittal024c0332010-02-03 11:44:00 -0800301
302SEND_RECOVERY_MSG:
Shashank Mittal024c0332010-02-03 11:44:00 -0800303 set_recovery_message(&msg); // send recovery message
304 boot_into_recovery = 1; // Boot in recovery mode
305 reboot_device(0);
306 return 0;
307}
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700308
309static int emmc_set_recovery_msg(struct recovery_message *out)
310{
311 char *ptn_name = "misc";
312 unsigned long long ptn = 0;
Mayank Grover9db97242017-06-16 12:41:56 +0530313 unsigned blocksize = mmc_get_device_blocksize();
314 unsigned int size = ROUND_TO_PAGE(sizeof(*out), (unsigned)blocksize - 1);
315 unsigned char *data = NULL;
316 int ret = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -0700317 int index = INVALID_PTN;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700318
Mayank Grover9db97242017-06-16 12:41:56 +0530319 data = malloc(size);
320 if(!data)
321 {
322 dprintf(CRITICAL,"memory allocation error \n");
323 ret = -1;
324 goto out;
325 }
326
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530327 index = partition_get_index((const char *) ptn_name);
Kinson Chikf1a43512011-07-14 11:28:39 -0700328 ptn = partition_get_offset(index);
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700329 mmc_set_lun(partition_get_lun(index));
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700330 if(ptn == 0) {
331 dprintf(CRITICAL,"partition %s doesn't exist\n",ptn_name);
Mayank Grover9db97242017-06-16 12:41:56 +0530332 ret = -1;
333 goto out;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700334 }
Mayank Grover9db97242017-06-16 12:41:56 +0530335 memset(data, 0, size);
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700336 memcpy(data, out, sizeof(*out));
337 if (mmc_write(ptn , size, (unsigned int*)data)) {
338 dprintf(CRITICAL,"mmc write failure %s %d\n",ptn_name, sizeof(*out));
Mayank Grover9db97242017-06-16 12:41:56 +0530339 ret = -1;
340 goto out;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700341 }
Mayank Grover9db97242017-06-16 12:41:56 +0530342out:
343 if (data)
344 free(data);
345 return ret;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700346}
347
348static int emmc_get_recovery_msg(struct recovery_message *in)
349{
350 char *ptn_name = "misc";
351 unsigned long long ptn = 0;
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700352 unsigned int size;
Kinson Chikf1a43512011-07-14 11:28:39 -0700353 int index = INVALID_PTN;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700354
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700355 size = mmc_get_device_blocksize();
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530356 index = partition_get_index((const char *) ptn_name);
Channagoud Kadabif2805632015-01-07 16:46:50 -0800357 if (index < 0)
358 {
359 dprintf(CRITICAL, "%s: Partition not found\n", ptn_name);
360 return -1;
361 }
362
Kinson Chikf1a43512011-07-14 11:28:39 -0700363 ptn = partition_get_offset(index);
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700364 mmc_set_lun(partition_get_lun(index));
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700365 if(ptn == 0) {
366 dprintf(CRITICAL,"partition %s doesn't exist\n",ptn_name);
367 return -1;
368 }
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700369 if (mmc_read(ptn , (unsigned int*)in, size)) {
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700370 dprintf(CRITICAL,"mmc read failure %s %d\n",ptn_name, size);
371 return -1;
372 }
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700373
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700374 return 0;
375}
376
377int _emmc_recovery_init(void)
378{
379 int update_status = 0;
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700380 struct recovery_message *msg;
381 uint32_t block_size = 0;
382
383 block_size = mmc_get_device_blocksize();
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700384
385 // get recovery message
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700386 msg = (struct recovery_message *)memalign(CACHE_LINE, block_size);
387 ASSERT(msg);
388
389 if(emmc_get_recovery_msg(msg))
390 {
391 if(msg)
392 free(msg);
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700393 return -1;
Pavel Nedev96a9aea2013-02-26 15:16:26 -0800394 }
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700395
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700396 msg->command[sizeof(msg->command)-1] = '\0'; //Ensure termination
397 if (msg->command[0] != 0 && msg->command[0] != 255) {
398 dprintf(INFO,"Recovery command: %d %s\n",
399 sizeof(msg->command), msg->command);
400 }
401
vijay kumar32e2e3c2014-08-01 21:10:28 +0530402 if (!strcmp(msg->command, "boot-recovery")) {
Stanimir Varbanovbe041992013-04-26 14:29:21 +0300403 boot_into_recovery = 1;
404 }
405
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700406 if (!strcmp("update-radio",msg->command))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700407 {
408 /* We're now here due to radio update, so check for update status */
Greg Griscod2471ef2011-07-14 13:00:42 -0700409 int ret = get_boot_info_apps(UPDATE_STATUS, (unsigned int *) &update_status);
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700410
411 if(!ret && (update_status & 0x01))
412 {
413 dprintf(INFO,"radio update success\n");
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700414 strlcpy(msg->status, "OKAY", sizeof(msg->status));
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700415 }
416 else
417 {
418 dprintf(INFO,"radio update failed\n");
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700419 strlcpy(msg->status, "failed-update", sizeof(msg->status));
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700420 }
Shashank Mittal162244e2011-08-08 19:01:25 -0700421 boot_into_recovery = 1; // Boot in recovery mode
422 }
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700423 if (!strcmp("reset-device-info",msg->command))
Shashank Mittal162244e2011-08-08 19:01:25 -0700424 {
425 reset_device_info();
426 }
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700427 if (!strcmp("root-detect",msg->command))
Shashank Mittal162244e2011-08-08 19:01:25 -0700428 {
429 set_device_root();
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700430 }
431 else
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700432 goto out;// do nothing
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700433
Sundarajan Srinivasan151c04d2014-07-17 15:52:12 -0700434 strlcpy(msg->command, "", sizeof(msg->command)); // clearing recovery command
435 emmc_set_recovery_msg(msg); // send recovery message
436
437out:
438 if(msg)
439 free(msg);
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -0700440 return 0;
441}
Pavel Nedeva1e62322013-04-05 15:21:36 +0300442
443static int read_misc(unsigned page_offset, void *buf, unsigned size)
444{
445 const char *ptn_name = "misc";
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700446 uint32_t pagesize = get_page_size();
Pavel Nedeva1e62322013-04-05 15:21:36 +0300447 unsigned offset;
Pavel Nedeva1e62322013-04-05 15:21:36 +0300448
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700449 if (size == 0 || buf == NULL)
Pavel Nedeva1e62322013-04-05 15:21:36 +0300450 return -1;
451
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700452 offset = page_offset * pagesize;
453
Pavel Nedeva1e62322013-04-05 15:21:36 +0300454 if (target_is_emmc_boot())
455 {
456 int index;
457 unsigned long long ptn;
458 unsigned long long ptn_size;
459
460 index = partition_get_index(ptn_name);
461 if (index == INVALID_PTN)
462 {
463 dprintf(CRITICAL, "No '%s' partition found\n", ptn_name);
464 return -1;
465 }
466
467 ptn = partition_get_offset(index);
468 ptn_size = partition_get_size(index);
469
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700470 mmc_set_lun(partition_get_lun(index));
471
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700472 if (ptn_size < offset + size)
Pavel Nedeva1e62322013-04-05 15:21:36 +0300473 {
474 dprintf(CRITICAL, "Read request out of '%s' boundaries\n",
475 ptn_name);
476 return -1;
477 }
478
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700479 if (mmc_read(ptn + offset, (unsigned int *)buf, size))
Pavel Nedeva1e62322013-04-05 15:21:36 +0300480 {
481 dprintf(CRITICAL, "Reading MMC failed\n");
482 return -1;
483 }
484 }
485 else
486 {
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700487 dprintf(CRITICAL, "Misc partition not supported for NAND targets.\n");
488 return -1;
Pavel Nedeva1e62322013-04-05 15:21:36 +0300489 }
490
Pavel Nedeva1e62322013-04-05 15:21:36 +0300491 return 0;
492}
493
Pavel Nedevc728bb32013-04-05 16:58:05 +0300494int write_misc(unsigned page_offset, void *buf, unsigned size)
495{
496 const char *ptn_name = "misc";
497 void *scratch_addr = target_get_scratch_address();
498 unsigned offset;
499 unsigned aligned_size;
500
501 if (size == 0 || buf == NULL || scratch_addr == NULL)
502 return -1;
503
504 if (target_is_emmc_boot())
505 {
506 int index;
507 unsigned long long ptn;
508 unsigned long long ptn_size;
509
510 index = partition_get_index(ptn_name);
511 if (index == INVALID_PTN)
512 {
513 dprintf(CRITICAL, "No '%s' partition found\n", ptn_name);
514 return -1;
515 }
516
517 ptn = partition_get_offset(index);
518 ptn_size = partition_get_size(index);
519
520 offset = page_offset * BLOCK_SIZE;
521 aligned_size = ROUND_TO_PAGE(size, (unsigned)BLOCK_SIZE - 1);
522 if (ptn_size < offset + aligned_size)
523 {
524 dprintf(CRITICAL, "Write request out of '%s' boundaries\n",
525 ptn_name);
526 return -1;
527 }
528
Mayank Grover467bf492017-10-24 12:27:53 +0530529 /* This will ensure, we zeored out any extra bytes
530 we will push to emmc, to prevent information leak */
531 if (aligned_size > size)
532 memset((scratch_addr + size), 0, (aligned_size-size));
533
Pavel Nedevc728bb32013-04-05 16:58:05 +0300534 if (scratch_addr != buf)
535 memcpy(scratch_addr, buf, size);
Channagoud Kadabi2776b652015-08-28 15:41:26 -0700536
537 /* Set Lun for misc partition */
538 mmc_set_lun(partition_get_lun(index));
539
Pavel Nedevc728bb32013-04-05 16:58:05 +0300540 if (mmc_write(ptn + offset, aligned_size, (unsigned int *)scratch_addr))
541 {
542 dprintf(CRITICAL, "Writing MMC failed\n");
543 return -1;
544 }
545 }
546 else
547 {
548 struct ptentry *ptn;
549 struct ptable *ptable;
550 unsigned pagesize = flash_page_size();
551
552 ptable = flash_get_ptable();
553 if (ptable == NULL)
554 {
555 dprintf(CRITICAL, "Partition table not found\n");
556 return -1;
557 }
558
559 ptn = ptable_find(ptable, ptn_name);
560 if (ptn == NULL)
561 {
562 dprintf(CRITICAL, "No '%s' partition found\n", ptn_name);
563 return -1;
564 }
565
566 offset = page_offset * pagesize;
567 aligned_size = ROUND_TO_PAGE(size, pagesize - 1);
568 if (ptn->length < offset + aligned_size)
569 {
570 dprintf(CRITICAL, "Write request out of '%s' boundaries\n",
571 ptn_name);
572 return -1;
573 }
574
Mayank Grover467bf492017-10-24 12:27:53 +0530575 /* This will ensure, we zeored out any extra bytes
576 we will push, to prevent information leak */
577 if (aligned_size > size)
578 memset((scratch_addr + size), 0, (aligned_size-size));
579
Pavel Nedevc728bb32013-04-05 16:58:05 +0300580 if (scratch_addr != buf)
581 memcpy(scratch_addr, buf, size);
Mayank Grover467bf492017-10-24 12:27:53 +0530582
Pavel Nedevc728bb32013-04-05 16:58:05 +0300583 if (flash_write(ptn, offset, scratch_addr, aligned_size)) {
584 dprintf(CRITICAL, "Writing flash failed\n");
585 return -1;
586 }
587 }
588
589 return 0;
590}
591
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700592int get_ffbm(char *ffbm, unsigned size)
Pavel Nedeva1e62322013-04-05 15:21:36 +0300593{
594 const char *ffbm_cmd = "ffbm-";
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700595 uint32_t page_size = get_page_size();
596 char *ffbm_page_buffer = NULL;
597 int retval = 0;
598 if (size < FFBM_MODE_BUF_SIZE || size >= page_size)
Pavel Nedeva1e62322013-04-05 15:21:36 +0300599 {
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700600 dprintf(CRITICAL, "Invalid size argument passed to get_ffbm\n");
601 retval = -1;
602 goto cleanup;
Pavel Nedeva1e62322013-04-05 15:21:36 +0300603 }
Parth Dixit75b16812016-04-05 19:17:17 +0530604 ffbm_page_buffer = (char*)memalign(CACHE_LINE, page_size);
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700605 if (!ffbm_page_buffer)
606 {
607 dprintf(CRITICAL, "Failed to alloc buffer for ffbm cookie\n");
608 retval = -1;
609 goto cleanup;
610 }
611 if (read_misc(0, ffbm_page_buffer, page_size))
Pavel Nedeva1e62322013-04-05 15:21:36 +0300612 {
613 dprintf(CRITICAL, "Error reading MISC partition\n");
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700614 retval = -1;
615 goto cleanup;
Pavel Nedeva1e62322013-04-05 15:21:36 +0300616 }
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700617 ffbm_page_buffer[size] = '\0';
618 if (strncmp(ffbm_cmd, ffbm_page_buffer, strlen(ffbm_cmd)))
619 {
620 retval = 0;
621 goto cleanup;
622 }
623 else
624 {
625 if (strlcpy(ffbm, ffbm_page_buffer, size) <
626 FFBM_MODE_BUF_SIZE -1)
627 {
628 dprintf(CRITICAL, "Invalid string in misc partition\n");
629 retval = -1;
630 }
631 else
632 retval = 1;
633 }
634cleanup:
635 if(ffbm_page_buffer)
636 free(ffbm_page_buffer);
637 return retval;
Pavel Nedeva1e62322013-04-05 15:21:36 +0300638}
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700639
640