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