The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 17 | extern "C" { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 18 | #include "mtdutils/mtdutils.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 19 | } |
Ethan Yonker | 34ae483 | 2016-08-24 15:32:18 -0500 | [diff] [blame] | 20 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 21 | #include <errno.h> |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 22 | #include <fcntl.h> |
| 23 | #include <inttypes.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 24 | #include <stdio.h> |
| 25 | #include <string.h> |
Doug Zongker | cfd256a | 2011-04-22 09:26:44 -0700 | [diff] [blame] | 26 | #include <sys/stat.h> |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 27 | #include <sys/types.h> |
Doug Zongker | cfd256a | 2011-04-22 09:26:44 -0700 | [diff] [blame] | 28 | #include <unistd.h> |
Ethan Yonker | c798c9c | 2015-10-09 11:15:26 -0500 | [diff] [blame] | 29 | #include <stdlib.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 30 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 31 | #include "bootloader.h" |
| 32 | #include "common.h" |
| 33 | #include "mtdutils/mtdutils.h" |
Ethan Yonker | 34ae483 | 2016-08-24 15:32:18 -0500 | [diff] [blame] | 34 | //#include "roots.h" |
Ethan Yonker | f117962 | 2016-08-25 15:32:21 -0500 | [diff] [blame] | 35 | //#include "unique_fd.h" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 36 | |
that | 4e0e3fc | 2015-04-13 19:52:49 +0200 | [diff] [blame] | 37 | // fake Volume struct that allows us to use the AOSP code easily |
| 38 | struct Volume |
| 39 | { |
| 40 | char fs_type[8]; |
| 41 | char blk_device[256]; |
| 42 | }; |
Dees_Troy | 1669f89 | 2013-09-04 18:35:08 +0000 | [diff] [blame] | 43 | |
that | 4e0e3fc | 2015-04-13 19:52:49 +0200 | [diff] [blame] | 44 | static Volume misc; |
| 45 | |
| 46 | void set_misc_device(const char* type, const char* name) { |
| 47 | strlcpy(misc.fs_type, type, sizeof(misc.fs_type)); |
| 48 | if (strlen(name) >= sizeof(misc.blk_device)) { |
| 49 | LOGE("New device name of '%s' is too large for bootloader.cpp\n", name); |
| 50 | } else { |
| 51 | strcpy(misc.blk_device, name); |
| 52 | } |
| 53 | } |
| 54 | |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 55 | static int get_bootloader_message_mtd(bootloader_message* out, const Volume* v); |
| 56 | static int set_bootloader_message_mtd(const bootloader_message* in, const Volume* v); |
| 57 | static int get_bootloader_message_block(bootloader_message* out, const Volume* v); |
| 58 | static int set_bootloader_message_block(const bootloader_message* in, const Volume* v); |
that | 4e0e3fc | 2015-04-13 19:52:49 +0200 | [diff] [blame] | 59 | |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 60 | int get_bootloader_message(bootloader_message* out) { |
that | 4e0e3fc | 2015-04-13 19:52:49 +0200 | [diff] [blame] | 61 | #if 0 |
| 62 | Volume* v = volume_for_path("/misc"); |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 63 | if (v == nullptr) { |
| 64 | LOGE("Cannot load volume /misc!\n"); |
| 65 | return -1; |
Adam Bliss | b2ceb69 | 2011-07-13 15:13:54 -0700 | [diff] [blame] | 66 | } |
that | 4e0e3fc | 2015-04-13 19:52:49 +0200 | [diff] [blame] | 67 | #else |
| 68 | Volume* v = &misc; |
| 69 | if (v->fs_type[0] == 0) { |
| 70 | LOGI("Not using /misc, not defined in fstab.\n"); |
| 71 | return -1; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 72 | } |
that | 4e0e3fc | 2015-04-13 19:52:49 +0200 | [diff] [blame] | 73 | #endif |
| 74 | if (strcmp(v->fs_type, "mtd") == 0) { |
| 75 | return get_bootloader_message_mtd(out, v); |
| 76 | } else if (strcmp(v->fs_type, "emmc") == 0) { |
| 77 | return get_bootloader_message_block(out, v); |
| 78 | } |
| 79 | LOGE("unknown misc partition fs_type \"%s\"\n", v->fs_type); |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 80 | return -1; |
Doug Zongker | 04611da | 2010-08-12 15:35:29 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 83 | int set_bootloader_message(const bootloader_message* in) { |
that | 4e0e3fc | 2015-04-13 19:52:49 +0200 | [diff] [blame] | 84 | #if 0 |
| 85 | Volume* v = volume_for_path("/misc"); |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 86 | if (v == nullptr) { |
| 87 | LOGE("Cannot load volume /misc!\n"); |
| 88 | return -1; |
Adam Bliss | b2ceb69 | 2011-07-13 15:13:54 -0700 | [diff] [blame] | 89 | } |
that | 4e0e3fc | 2015-04-13 19:52:49 +0200 | [diff] [blame] | 90 | #else |
| 91 | Volume* v = &misc; |
| 92 | if (v->fs_type[0] == 0) { |
| 93 | LOGI("Not using /misc, not defined in fstab.\n"); |
| 94 | return -1; |
Doug Zongker | 04611da | 2010-08-12 15:35:29 -0700 | [diff] [blame] | 95 | } |
that | 4e0e3fc | 2015-04-13 19:52:49 +0200 | [diff] [blame] | 96 | #endif |
| 97 | if (strcmp(v->fs_type, "mtd") == 0) { |
| 98 | return set_bootloader_message_mtd(in, v); |
| 99 | } else if (strcmp(v->fs_type, "emmc") == 0) { |
| 100 | return set_bootloader_message_block(in, v); |
| 101 | } |
| 102 | LOGE("unknown misc partition fs_type \"%s\"\n", v->fs_type); |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 103 | return -1; |
Doug Zongker | 04611da | 2010-08-12 15:35:29 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 106 | // ------------------------------ |
| 107 | // for misc partitions on MTD |
| 108 | // ------------------------------ |
Doug Zongker | 04611da | 2010-08-12 15:35:29 -0700 | [diff] [blame] | 109 | |
| 110 | static const int MISC_PAGES = 3; // number of pages to save |
| 111 | static const int MISC_COMMAND_PAGE = 1; // bootloader command is this page |
that | 4e0e3fc | 2015-04-13 19:52:49 +0200 | [diff] [blame] | 112 | |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 113 | static int get_bootloader_message_mtd(bootloader_message* out, |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 114 | const Volume* v) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 115 | size_t write_size; |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 116 | mtd_scan_partitions(); |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 117 | const MtdPartition* part = mtd_find_partition_by_name(v->blk_device); |
| 118 | if (part == nullptr || mtd_partition_info(part, nullptr, nullptr, &write_size)) { |
| 119 | LOGE("failed to find \"%s\"\n", v->blk_device); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 120 | return -1; |
| 121 | } |
| 122 | |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 123 | MtdReadContext* read = mtd_read_partition(part); |
| 124 | if (read == nullptr) { |
| 125 | LOGE("failed to open \"%s\": %s\n", v->blk_device, strerror(errno)); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 126 | return -1; |
| 127 | } |
| 128 | |
| 129 | const ssize_t size = write_size * MISC_PAGES; |
| 130 | char data[size]; |
| 131 | ssize_t r = mtd_read_data(read, data, size); |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 132 | if (r != size) LOGE("failed to read \"%s\": %s\n", v->blk_device, strerror(errno)); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 133 | mtd_read_close(read); |
| 134 | if (r != size) return -1; |
| 135 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 136 | memcpy(out, &data[write_size * MISC_COMMAND_PAGE], sizeof(*out)); |
| 137 | return 0; |
| 138 | } |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 139 | static int set_bootloader_message_mtd(const bootloader_message* in, |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 140 | const Volume* v) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 141 | size_t write_size; |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 142 | mtd_scan_partitions(); |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 143 | const MtdPartition* part = mtd_find_partition_by_name(v->blk_device); |
| 144 | if (part == nullptr || mtd_partition_info(part, nullptr, nullptr, &write_size)) { |
| 145 | LOGE("failed to find \"%s\"\n", v->blk_device); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 146 | return -1; |
| 147 | } |
| 148 | |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 149 | MtdReadContext* read = mtd_read_partition(part); |
| 150 | if (read == nullptr) { |
| 151 | LOGE("failed to open \"%s\": %s\n", v->blk_device, strerror(errno)); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 152 | return -1; |
| 153 | } |
| 154 | |
| 155 | ssize_t size = write_size * MISC_PAGES; |
| 156 | char data[size]; |
| 157 | ssize_t r = mtd_read_data(read, data, size); |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 158 | if (r != size) LOGE("failed to read \"%s\": %s\n", v->blk_device, strerror(errno)); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 159 | mtd_read_close(read); |
| 160 | if (r != size) return -1; |
| 161 | |
| 162 | memcpy(&data[write_size * MISC_COMMAND_PAGE], in, sizeof(*in)); |
| 163 | |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 164 | MtdWriteContext* write = mtd_write_partition(part); |
| 165 | if (write == nullptr) { |
| 166 | LOGE("failed to open \"%s\": %s\n", v->blk_device, strerror(errno)); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 167 | return -1; |
| 168 | } |
| 169 | if (mtd_write_data(write, data, size) != size) { |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 170 | LOGE("failed to write \"%s\": %s\n", v->blk_device, strerror(errno)); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 171 | mtd_write_close(write); |
| 172 | return -1; |
| 173 | } |
| 174 | if (mtd_write_close(write)) { |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 175 | LOGE("failed to finish \"%s\": %s\n", v->blk_device, strerror(errno)); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 176 | return -1; |
| 177 | } |
| 178 | |
| 179 | LOGI("Set boot command \"%s\"\n", in->command[0] != 255 ? in->command : ""); |
| 180 | return 0; |
| 181 | } |
Doug Zongker | 04611da | 2010-08-12 15:35:29 -0700 | [diff] [blame] | 182 | |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 183 | |
| 184 | // ------------------------------------ |
| 185 | // for misc partitions on block devices |
| 186 | // ------------------------------------ |
| 187 | |
Doug Zongker | cfd256a | 2011-04-22 09:26:44 -0700 | [diff] [blame] | 188 | static void wait_for_device(const char* fn) { |
| 189 | int tries = 0; |
| 190 | int ret; |
Doug Zongker | cfd256a | 2011-04-22 09:26:44 -0700 | [diff] [blame] | 191 | do { |
| 192 | ++tries; |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 193 | struct stat buf; |
Doug Zongker | cfd256a | 2011-04-22 09:26:44 -0700 | [diff] [blame] | 194 | ret = stat(fn, &buf); |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 195 | if (ret == -1) { |
| 196 | printf("failed to stat \"%s\" try %d: %s\n", fn, tries, strerror(errno)); |
Doug Zongker | cfd256a | 2011-04-22 09:26:44 -0700 | [diff] [blame] | 197 | sleep(1); |
| 198 | } |
| 199 | } while (ret && tries < 10); |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 200 | |
Doug Zongker | cfd256a | 2011-04-22 09:26:44 -0700 | [diff] [blame] | 201 | if (ret) { |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 202 | printf("failed to stat \"%s\"\n", fn); |
Doug Zongker | cfd256a | 2011-04-22 09:26:44 -0700 | [diff] [blame] | 203 | } |
| 204 | } |
that | 4e0e3fc | 2015-04-13 19:52:49 +0200 | [diff] [blame] | 205 | |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 206 | static int get_bootloader_message_block(bootloader_message* out, |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 207 | const Volume* v) { |
Ken Sumrall | f35d1ce | 2013-02-13 12:59:35 -0800 | [diff] [blame] | 208 | wait_for_device(v->blk_device); |
| 209 | FILE* f = fopen(v->blk_device, "rb"); |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 210 | if (f == nullptr) { |
| 211 | LOGE("failed to open \"%s\": %s\n", v->blk_device, strerror(errno)); |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 212 | return -1; |
| 213 | } |
Matt Mower | fb60a94 | 2014-07-08 22:25:38 -0500 | [diff] [blame] | 214 | #ifdef BOARD_RECOVERY_BLDRMSG_OFFSET |
| 215 | fseek(f, BOARD_RECOVERY_BLDRMSG_OFFSET, SEEK_SET); |
| 216 | #endif |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 217 | bootloader_message temp; |
Ethan Yonker | 34ae483 | 2016-08-24 15:32:18 -0500 | [diff] [blame] | 218 | |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 219 | int count = fread(&temp, sizeof(temp), 1, f); |
| 220 | if (count != 1) { |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 221 | LOGE("failed to read \"%s\": %s\n", v->blk_device, strerror(errno)); |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 222 | return -1; |
| 223 | } |
| 224 | if (fclose(f) != 0) { |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 225 | LOGE("failed to close \"%s\": %s\n", v->blk_device, strerror(errno)); |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 226 | return -1; |
| 227 | } |
| 228 | memcpy(out, &temp, sizeof(temp)); |
| 229 | return 0; |
| 230 | } |
| 231 | |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 232 | static int set_bootloader_message_block(const bootloader_message* in, |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 233 | const Volume* v) { |
Ken Sumrall | f35d1ce | 2013-02-13 12:59:35 -0800 | [diff] [blame] | 234 | wait_for_device(v->blk_device); |
Ethan Yonker | f117962 | 2016-08-25 15:32:21 -0500 | [diff] [blame] | 235 | int fd = open(v->blk_device, O_WRONLY | O_SYNC); |
| 236 | if (fd == -1) { |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 237 | LOGE("failed to open \"%s\": %s\n", v->blk_device, strerror(errno)); |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 238 | return -1; |
| 239 | } |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 240 | |
Matt Mower | fb60a94 | 2014-07-08 22:25:38 -0500 | [diff] [blame] | 241 | #ifdef BOARD_RECOVERY_BLDRMSG_OFFSET |
Ethan Yonker | f117962 | 2016-08-25 15:32:21 -0500 | [diff] [blame] | 242 | lseek(fd, BOARD_RECOVERY_BLDRMSG_OFFSET, SEEK_SET); |
Matt Mower | fb60a94 | 2014-07-08 22:25:38 -0500 | [diff] [blame] | 243 | #endif |
Ethan Yonker | 34ae483 | 2016-08-24 15:32:18 -0500 | [diff] [blame] | 244 | |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 245 | size_t written = 0; |
| 246 | const uint8_t* start = reinterpret_cast<const uint8_t*>(in); |
| 247 | size_t total = sizeof(*in); |
| 248 | while (written < total) { |
Ethan Yonker | f117962 | 2016-08-25 15:32:21 -0500 | [diff] [blame] | 249 | ssize_t wrote = TEMP_FAILURE_RETRY(write(fd, start + written, total - written)); |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 250 | if (wrote == -1) { |
Ethan Yonker | f117962 | 2016-08-25 15:32:21 -0500 | [diff] [blame] | 251 | LOGE("failed to write some bytes: %s\n", |
| 252 | strerror(errno)); |
| 253 | close(fd); |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 254 | return -1; |
| 255 | } |
| 256 | written += wrote; |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 257 | } |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 258 | |
Ethan Yonker | f117962 | 2016-08-25 15:32:21 -0500 | [diff] [blame] | 259 | if (fsync(fd) == -1) { |
Tao Bao | bd82b27 | 2016-01-21 13:49:03 -0800 | [diff] [blame] | 260 | LOGE("failed to fsync \"%s\": %s\n", v->blk_device, strerror(errno)); |
Ethan Yonker | f117962 | 2016-08-25 15:32:21 -0500 | [diff] [blame] | 261 | close(fd); |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 262 | return -1; |
| 263 | } |
Ethan Yonker | f117962 | 2016-08-25 15:32:21 -0500 | [diff] [blame] | 264 | close(fd); |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 265 | return 0; |
| 266 | } |
Dees_Troy | 1669f89 | 2013-09-04 18:35:08 +0000 | [diff] [blame] | 267 | |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 268 | |
| 269 | static const char *COMMAND_FILE = "/cache/recovery/command"; |
| 270 | static const int MAX_ARG_LENGTH = 4096; |
| 271 | static const int MAX_ARGS = 100; |
| 272 | |
| 273 | // command line args come from, in decreasing precedence: |
| 274 | // - the actual command line |
| 275 | // - the bootloader control block (one per line, after "recovery") |
| 276 | // - the contents of COMMAND_FILE (one per line) |
| 277 | void |
| 278 | get_args(int *argc, char ***argv) { |
| 279 | struct bootloader_message boot; |
| 280 | memset(&boot, 0, sizeof(boot)); |
| 281 | get_bootloader_message(&boot); // this may fail, leaving a zeroed structure |
| 282 | |
| 283 | if (boot.command[0] != 0 && boot.command[0] != 255) { |
that | 4e0e3fc | 2015-04-13 19:52:49 +0200 | [diff] [blame] | 284 | LOGI("Boot command: %.*s\n", (int)sizeof(boot.command), boot.command); |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | if (boot.status[0] != 0 && boot.status[0] != 255) { |
that | 4e0e3fc | 2015-04-13 19:52:49 +0200 | [diff] [blame] | 288 | LOGI("Boot status: %.*s\n", (int)sizeof(boot.status), boot.status); |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | // --- if arguments weren't supplied, look in the bootloader control block |
| 292 | if (*argc <= 1) { |
| 293 | boot.recovery[sizeof(boot.recovery) - 1] = '\0'; // Ensure termination |
| 294 | const char *arg = strtok(boot.recovery, "\n"); |
| 295 | if (arg != NULL && !strcmp(arg, "recovery")) { |
| 296 | *argv = (char **) malloc(sizeof(char *) * MAX_ARGS); |
| 297 | (*argv)[0] = strdup(arg); |
| 298 | for (*argc = 1; *argc < MAX_ARGS; ++*argc) { |
| 299 | if ((arg = strtok(NULL, "\n")) == NULL) break; |
| 300 | (*argv)[*argc] = strdup(arg); |
| 301 | } |
| 302 | LOGI("Got arguments from boot message\n"); |
| 303 | } else if (boot.recovery[0] != 0 && boot.recovery[0] != 255) { |
| 304 | LOGE("Bad boot message\n\"%.20s\"\n", boot.recovery); |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | // --- if that doesn't work, try the command file |
| 309 | if (*argc <= 1) { |
| 310 | FILE *fp = fopen(COMMAND_FILE, "r"); |
| 311 | if (fp != NULL) { |
that | 9eadc51 | 2015-03-27 00:06:51 +0100 | [diff] [blame] | 312 | char *token; |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 313 | char *argv0 = (*argv)[0]; |
| 314 | *argv = (char **) malloc(sizeof(char *) * MAX_ARGS); |
| 315 | (*argv)[0] = argv0; // use the same program name |
| 316 | |
| 317 | char buf[MAX_ARG_LENGTH]; |
| 318 | for (*argc = 1; *argc < MAX_ARGS; ++*argc) { |
| 319 | if (!fgets(buf, sizeof(buf), fp)) break; |
that | 9eadc51 | 2015-03-27 00:06:51 +0100 | [diff] [blame] | 320 | token = strtok(buf, "\r\n"); |
| 321 | if (token != NULL) { |
| 322 | (*argv)[*argc] = strdup(token); // Strip newline. |
| 323 | } else { |
| 324 | --*argc; |
| 325 | } |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | fflush(fp); |
that | 9eadc51 | 2015-03-27 00:06:51 +0100 | [diff] [blame] | 329 | if (ferror(fp)) LOGE("Error in %s\n(%s)\n", COMMAND_FILE, strerror(errno)); |
| 330 | fclose(fp); |
Dees_Troy | a449a6f | 2013-04-07 17:50:11 -0500 | [diff] [blame] | 331 | LOGI("Got arguments from %s\n", COMMAND_FILE); |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | // --> write the arguments we have back into the bootloader control block |
| 336 | // always boot into recovery after this (until finish_recovery() is called) |
| 337 | strlcpy(boot.command, "boot-recovery", sizeof(boot.command)); |
| 338 | strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery)); |
| 339 | int i; |
| 340 | for (i = 1; i < *argc; ++i) { |
| 341 | strlcat(boot.recovery, (*argv)[i], sizeof(boot.recovery)); |
| 342 | strlcat(boot.recovery, "\n", sizeof(boot.recovery)); |
| 343 | } |
| 344 | set_bootloader_message(&boot); |
| 345 | } |