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 | |
| 17 | #include "bootloader.h" |
| 18 | #include "common.h" |
| 19 | #include "mtdutils/mtdutils.h" |
| 20 | #include "roots.h" |
| 21 | |
| 22 | #include <errno.h> |
| 23 | #include <stdio.h> |
| 24 | #include <string.h> |
| 25 | |
| 26 | static const char *CACHE_NAME = "CACHE:"; |
| 27 | static const char *MISC_NAME = "MISC:"; |
| 28 | static const int MISC_PAGES = 3; // number of pages to save |
| 29 | static const int MISC_COMMAND_PAGE = 1; // bootloader command is this page |
| 30 | |
| 31 | #ifdef LOG_VERBOSE |
| 32 | static void dump_data(const char *data, int len) { |
| 33 | int pos; |
| 34 | for (pos = 0; pos < len; ) { |
| 35 | printf("%05x: %02x", pos, data[pos]); |
| 36 | for (++pos; pos < len && (pos % 24) != 0; ++pos) { |
| 37 | printf(" %02x", data[pos]); |
| 38 | } |
| 39 | printf("\n"); |
| 40 | } |
| 41 | } |
| 42 | #endif |
| 43 | |
| 44 | int get_bootloader_message(struct bootloader_message *out) { |
| 45 | size_t write_size; |
| 46 | const MtdPartition *part = get_root_mtd_partition(MISC_NAME); |
| 47 | if (part == NULL || mtd_partition_info(part, NULL, NULL, &write_size)) { |
| 48 | LOGE("Can't find %s\n", MISC_NAME); |
| 49 | return -1; |
| 50 | } |
| 51 | |
| 52 | MtdReadContext *read = mtd_read_partition(part); |
| 53 | if (read == NULL) { |
| 54 | LOGE("Can't open %s\n(%s)\n", MISC_NAME, strerror(errno)); |
| 55 | return -1; |
| 56 | } |
| 57 | |
| 58 | const ssize_t size = write_size * MISC_PAGES; |
| 59 | char data[size]; |
| 60 | ssize_t r = mtd_read_data(read, data, size); |
| 61 | if (r != size) LOGE("Can't read %s\n(%s)\n", MISC_NAME, strerror(errno)); |
| 62 | mtd_read_close(read); |
| 63 | if (r != size) return -1; |
| 64 | |
| 65 | #ifdef LOG_VERBOSE |
| 66 | printf("\n--- get_bootloader_message ---\n"); |
| 67 | dump_data(data, size); |
| 68 | printf("\n"); |
| 69 | #endif |
| 70 | |
| 71 | memcpy(out, &data[write_size * MISC_COMMAND_PAGE], sizeof(*out)); |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | int set_bootloader_message(const struct bootloader_message *in) { |
| 76 | size_t write_size; |
| 77 | const MtdPartition *part = get_root_mtd_partition(MISC_NAME); |
| 78 | if (part == NULL || mtd_partition_info(part, NULL, NULL, &write_size)) { |
| 79 | LOGE("Can't find %s\n", MISC_NAME); |
| 80 | return -1; |
| 81 | } |
| 82 | |
| 83 | MtdReadContext *read = mtd_read_partition(part); |
| 84 | if (read == NULL) { |
| 85 | LOGE("Can't open %s\n(%s)\n", MISC_NAME, strerror(errno)); |
| 86 | return -1; |
| 87 | } |
| 88 | |
| 89 | ssize_t size = write_size * MISC_PAGES; |
| 90 | char data[size]; |
| 91 | ssize_t r = mtd_read_data(read, data, size); |
| 92 | if (r != size) LOGE("Can't read %s\n(%s)\n", MISC_NAME, strerror(errno)); |
| 93 | mtd_read_close(read); |
| 94 | if (r != size) return -1; |
| 95 | |
| 96 | memcpy(&data[write_size * MISC_COMMAND_PAGE], in, sizeof(*in)); |
| 97 | |
| 98 | #ifdef LOG_VERBOSE |
| 99 | printf("\n--- set_bootloader_message ---\n"); |
| 100 | dump_data(data, size); |
| 101 | printf("\n"); |
| 102 | #endif |
| 103 | |
| 104 | MtdWriteContext *write = mtd_write_partition(part); |
| 105 | if (write == NULL) { |
| 106 | LOGE("Can't open %s\n(%s)\n", MISC_NAME, strerror(errno)); |
| 107 | return -1; |
| 108 | } |
| 109 | if (mtd_write_data(write, data, size) != size) { |
| 110 | LOGE("Can't write %s\n(%s)\n", MISC_NAME, strerror(errno)); |
| 111 | mtd_write_close(write); |
| 112 | return -1; |
| 113 | } |
| 114 | if (mtd_write_close(write)) { |
| 115 | LOGE("Can't finish %s\n(%s)\n", MISC_NAME, strerror(errno)); |
| 116 | return -1; |
| 117 | } |
| 118 | |
| 119 | LOGI("Set boot command \"%s\"\n", in->command[0] != 255 ? in->command : ""); |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | /* Update Image |
| 124 | * |
| 125 | * - will be stored in the "cache" partition |
| 126 | * - bad blocks will be ignored, like boot.img and recovery.img |
| 127 | * - the first block will be the image header (described below) |
| 128 | * - the size is in BYTES, inclusive of the header |
| 129 | * - offsets are in BYTES from the start of the update header |
| 130 | * - two raw bitmaps will be included, the "busy" and "fail" bitmaps |
| 131 | * - for dream, the bitmaps will be 320x480x16bpp RGB565 |
| 132 | */ |
| 133 | |
| 134 | #define UPDATE_MAGIC "MSM-RADIO-UPDATE" |
| 135 | #define UPDATE_MAGIC_SIZE 16 |
| 136 | #define UPDATE_VERSION 0x00010000 |
| 137 | |
| 138 | struct update_header { |
| 139 | unsigned char MAGIC[UPDATE_MAGIC_SIZE]; |
| 140 | |
| 141 | unsigned version; |
| 142 | unsigned size; |
| 143 | |
| 144 | unsigned image_offset; |
| 145 | unsigned image_length; |
| 146 | |
| 147 | unsigned bitmap_width; |
| 148 | unsigned bitmap_height; |
| 149 | unsigned bitmap_bpp; |
| 150 | |
| 151 | unsigned busy_bitmap_offset; |
| 152 | unsigned busy_bitmap_length; |
| 153 | |
| 154 | unsigned fail_bitmap_offset; |
| 155 | unsigned fail_bitmap_length; |
| 156 | }; |
| 157 | |
Doug Zongker | 687bc12 | 2010-01-20 16:34:10 -0800 | [diff] [blame^] | 158 | #define LOG_MAGIC "LOGmagic" |
| 159 | #define LOG_MAGIC_SIZE 8 |
| 160 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 161 | int write_update_for_bootloader( |
| 162 | const char *update, int update_length, |
| 163 | int bitmap_width, int bitmap_height, int bitmap_bpp, |
Doug Zongker | 687bc12 | 2010-01-20 16:34:10 -0800 | [diff] [blame^] | 164 | const char *busy_bitmap, const char *fail_bitmap, |
| 165 | const char *log_filename) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 166 | if (ensure_root_path_unmounted(CACHE_NAME)) { |
| 167 | LOGE("Can't unmount %s\n", CACHE_NAME); |
| 168 | return -1; |
| 169 | } |
| 170 | |
| 171 | const MtdPartition *part = get_root_mtd_partition(CACHE_NAME); |
| 172 | if (part == NULL) { |
| 173 | LOGE("Can't find %s\n", CACHE_NAME); |
| 174 | return -1; |
| 175 | } |
| 176 | |
| 177 | MtdWriteContext *write = mtd_write_partition(part); |
| 178 | if (write == NULL) { |
| 179 | LOGE("Can't open %s\n(%s)\n", CACHE_NAME, strerror(errno)); |
| 180 | return -1; |
| 181 | } |
| 182 | |
| 183 | /* Write an invalid (zero) header first, to disable any previous |
| 184 | * update and any other structured contents (like a filesystem), |
| 185 | * and as a placeholder for the amount of space required. |
| 186 | */ |
| 187 | |
| 188 | struct update_header header; |
| 189 | memset(&header, 0, sizeof(header)); |
| 190 | const ssize_t header_size = sizeof(header); |
| 191 | if (mtd_write_data(write, (char*) &header, header_size) != header_size) { |
| 192 | LOGE("Can't write header to %s\n(%s)\n", CACHE_NAME, strerror(errno)); |
| 193 | mtd_write_close(write); |
| 194 | return -1; |
| 195 | } |
| 196 | |
| 197 | /* Write each section individually block-aligned, so we can write |
| 198 | * each block independently without complicated buffering. |
| 199 | */ |
| 200 | |
| 201 | memcpy(&header.MAGIC, UPDATE_MAGIC, UPDATE_MAGIC_SIZE); |
| 202 | header.version = UPDATE_VERSION; |
| 203 | header.size = header_size; |
| 204 | |
Doug Zongker | 687bc12 | 2010-01-20 16:34:10 -0800 | [diff] [blame^] | 205 | if (log_filename != NULL) { |
| 206 | // Write 1 byte into the following block, then fill to the end |
| 207 | // in order to reserve that block. We'll use the block to |
| 208 | // send a copy of the log through to the next invocation of |
| 209 | // recovery. We write the log as late as possible in order to |
| 210 | // capture any messages emitted by this function. |
| 211 | mtd_erase_blocks(write, 0); |
| 212 | if (mtd_write_data(write, (char*) &header, 1) != 1) { |
| 213 | LOGE("Can't write log block to %s\n(%s)\n", |
| 214 | CACHE_NAME, strerror(errno)); |
| 215 | mtd_write_close(write); |
| 216 | return -1; |
| 217 | } |
| 218 | } |
| 219 | |
Doug Zongker | 4c5f9f3 | 2010-01-12 16:18:33 -0800 | [diff] [blame] | 220 | off_t image_start_pos = mtd_erase_blocks(write, 0); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 221 | header.image_length = update_length; |
| 222 | if ((int) header.image_offset == -1 || |
| 223 | mtd_write_data(write, update, update_length) != update_length) { |
| 224 | LOGE("Can't write update to %s\n(%s)\n", CACHE_NAME, strerror(errno)); |
| 225 | mtd_write_close(write); |
| 226 | return -1; |
| 227 | } |
Doug Zongker | 4c5f9f3 | 2010-01-12 16:18:33 -0800 | [diff] [blame] | 228 | off_t busy_start_pos = mtd_erase_blocks(write, 0); |
| 229 | header.image_offset = mtd_find_write_start(write, image_start_pos); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 230 | |
| 231 | header.bitmap_width = bitmap_width; |
| 232 | header.bitmap_height = bitmap_height; |
| 233 | header.bitmap_bpp = bitmap_bpp; |
| 234 | |
| 235 | int bitmap_length = (bitmap_bpp + 7) / 8 * bitmap_width * bitmap_height; |
| 236 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 237 | header.busy_bitmap_length = busy_bitmap != NULL ? bitmap_length : 0; |
| 238 | if ((int) header.busy_bitmap_offset == -1 || |
| 239 | mtd_write_data(write, busy_bitmap, bitmap_length) != bitmap_length) { |
| 240 | LOGE("Can't write bitmap to %s\n(%s)\n", CACHE_NAME, strerror(errno)); |
| 241 | mtd_write_close(write); |
| 242 | return -1; |
| 243 | } |
Doug Zongker | 4c5f9f3 | 2010-01-12 16:18:33 -0800 | [diff] [blame] | 244 | off_t fail_start_pos = mtd_erase_blocks(write, 0); |
| 245 | header.busy_bitmap_offset = mtd_find_write_start(write, busy_start_pos); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 246 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 247 | header.fail_bitmap_length = fail_bitmap != NULL ? bitmap_length : 0; |
| 248 | if ((int) header.fail_bitmap_offset == -1 || |
| 249 | mtd_write_data(write, fail_bitmap, bitmap_length) != bitmap_length) { |
| 250 | LOGE("Can't write bitmap to %s\n(%s)\n", CACHE_NAME, strerror(errno)); |
| 251 | mtd_write_close(write); |
| 252 | return -1; |
| 253 | } |
Doug Zongker | 4c5f9f3 | 2010-01-12 16:18:33 -0800 | [diff] [blame] | 254 | mtd_erase_blocks(write, 0); |
| 255 | header.fail_bitmap_offset = mtd_find_write_start(write, fail_start_pos); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 256 | |
| 257 | /* Write the header last, after all the blocks it refers to, so that |
| 258 | * when the magic number is installed everything is valid. |
| 259 | */ |
| 260 | |
| 261 | if (mtd_write_close(write)) { |
| 262 | LOGE("Can't finish writing %s\n(%s)\n", CACHE_NAME, strerror(errno)); |
| 263 | return -1; |
| 264 | } |
| 265 | |
| 266 | write = mtd_write_partition(part); |
| 267 | if (write == NULL) { |
| 268 | LOGE("Can't reopen %s\n(%s)\n", CACHE_NAME, strerror(errno)); |
| 269 | return -1; |
| 270 | } |
| 271 | |
| 272 | if (mtd_write_data(write, (char*) &header, header_size) != header_size) { |
| 273 | LOGE("Can't rewrite header to %s\n(%s)\n", CACHE_NAME, strerror(errno)); |
| 274 | mtd_write_close(write); |
| 275 | return -1; |
| 276 | } |
| 277 | |
Doug Zongker | 687bc12 | 2010-01-20 16:34:10 -0800 | [diff] [blame^] | 278 | if (log_filename != NULL) { |
| 279 | size_t erase_size; |
| 280 | if (mtd_partition_info(part, NULL, &erase_size, NULL) != 0) { |
| 281 | LOGE("Error reading block size\n(%s)\n", strerror(errno)); |
| 282 | mtd_write_close(write); |
| 283 | return -1; |
| 284 | } |
| 285 | mtd_erase_blocks(write, 0); |
| 286 | |
| 287 | if (erase_size > 0) { |
| 288 | char* log = malloc(erase_size); |
| 289 | FILE* f = fopen(log_filename, "rb"); |
| 290 | // The fseek() may fail if it tries to go before the |
| 291 | // beginning of the log, but that's okay because we want |
| 292 | // to be positioned at the start anyway. |
| 293 | fseek(f, -(erase_size-sizeof(size_t)-LOG_MAGIC_SIZE), SEEK_END); |
| 294 | memcpy(log, LOG_MAGIC, LOG_MAGIC_SIZE); |
| 295 | size_t read = fread(log+sizeof(size_t)+LOG_MAGIC_SIZE, |
| 296 | 1, erase_size-sizeof(size_t)-LOG_MAGIC_SIZE, f); |
| 297 | LOGI("read %d bytes from log\n", (int)read); |
| 298 | *(size_t *)(log + LOG_MAGIC_SIZE) = read; |
| 299 | fclose(f); |
| 300 | if (mtd_write_data(write, log, erase_size) != erase_size) { |
| 301 | LOGE("failed to store log in cache partition\n(%s)\n", |
| 302 | strerror(errno)); |
| 303 | mtd_write_close(write); |
| 304 | } |
| 305 | free(log); |
| 306 | } |
| 307 | } |
| 308 | |
Doug Zongker | 4c5f9f3 | 2010-01-12 16:18:33 -0800 | [diff] [blame] | 309 | if (mtd_erase_blocks(write, 0) != image_start_pos) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 310 | LOGE("Misalignment rewriting %s\n(%s)\n", CACHE_NAME, strerror(errno)); |
| 311 | mtd_write_close(write); |
| 312 | return -1; |
| 313 | } |
| 314 | |
| 315 | if (mtd_write_close(write)) { |
| 316 | LOGE("Can't finish header of %s\n(%s)\n", CACHE_NAME, strerror(errno)); |
| 317 | return -1; |
| 318 | } |
| 319 | |
| 320 | return 0; |
| 321 | } |
Doug Zongker | 687bc12 | 2010-01-20 16:34:10 -0800 | [diff] [blame^] | 322 | |
| 323 | void recover_firmware_update_log() { |
| 324 | printf("recovering log from before firmware update\n"); |
| 325 | |
| 326 | const MtdPartition *part = get_root_mtd_partition(CACHE_NAME); |
| 327 | if (part == NULL) { |
| 328 | LOGE("Can't find %s\n", CACHE_NAME); |
| 329 | return; |
| 330 | } |
| 331 | |
| 332 | MtdReadContext* read = mtd_read_partition(part); |
| 333 | |
| 334 | size_t erase_size; |
| 335 | if (mtd_partition_info(part, NULL, &erase_size, NULL) != 0) { |
| 336 | LOGE("Error reading block size\n(%s)\n", strerror(errno)); |
| 337 | mtd_read_close(read); |
| 338 | return; |
| 339 | } |
| 340 | |
| 341 | char* buffer = malloc(erase_size); |
| 342 | if (mtd_read_data(read, buffer, erase_size) != erase_size) { |
| 343 | LOGE("Error reading header block\n(%s)\n", strerror(errno)); |
| 344 | mtd_read_close(read); |
| 345 | free(buffer); |
| 346 | return; |
| 347 | } |
| 348 | if (mtd_read_data(read, buffer, erase_size) != erase_size) { |
| 349 | LOGE("Error reading log block\n(%s)\n", strerror(errno)); |
| 350 | mtd_read_close(read); |
| 351 | free(buffer); |
| 352 | return; |
| 353 | } |
| 354 | mtd_read_close(read); |
| 355 | |
| 356 | if (memcmp(buffer, LOG_MAGIC, LOG_MAGIC_SIZE) != 0) { |
| 357 | LOGE("No log from before firmware install\n"); |
| 358 | free(buffer); |
| 359 | return; |
| 360 | } |
| 361 | |
| 362 | size_t log_size = *(size_t *)(buffer + LOG_MAGIC_SIZE); |
| 363 | LOGI("header has %d bytes of log\n", (int)log_size); |
| 364 | |
| 365 | printf("\n###\n### START RECOVERED LOG\n###\n\n"); |
| 366 | fwrite(buffer + sizeof(size_t) + LOG_MAGIC_SIZE, 1, log_size, stdout); |
| 367 | printf("\n\n###\n### END RECOVERED LOG\n###\n\n"); |
| 368 | |
| 369 | free(buffer); |
| 370 | } |