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> |
Doug Zongker | cfd256a | 2011-04-22 09:26:44 -0700 | [diff] [blame] | 25 | #include <sys/stat.h> |
| 26 | #include <unistd.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 27 | |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 28 | static int get_bootloader_message_mtd(struct bootloader_message *out, const Volume* v); |
| 29 | static int set_bootloader_message_mtd(const struct bootloader_message *in, const Volume* v); |
| 30 | static int get_bootloader_message_block(struct bootloader_message *out, const Volume* v); |
| 31 | static int set_bootloader_message_block(const struct bootloader_message *in, const Volume* v); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 32 | |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 33 | int get_bootloader_message(struct bootloader_message *out) { |
| 34 | Volume* v = volume_for_path("/misc"); |
| 35 | if (strcmp(v->fs_type, "mtd") == 0) { |
| 36 | return get_bootloader_message_mtd(out, v); |
| 37 | } else if (strcmp(v->fs_type, "emmc") == 0) { |
| 38 | return get_bootloader_message_block(out, v); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 39 | } |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 40 | LOGE("unknown misc partition fs_type \"%s\"\n", v->fs_type); |
| 41 | return -1; |
Doug Zongker | 04611da | 2010-08-12 15:35:29 -0700 | [diff] [blame] | 42 | } |
| 43 | |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 44 | int set_bootloader_message(const struct bootloader_message *in) { |
| 45 | Volume* v = volume_for_path("/misc"); |
| 46 | if (strcmp(v->fs_type, "mtd") == 0) { |
| 47 | return set_bootloader_message_mtd(in, v); |
| 48 | } else if (strcmp(v->fs_type, "emmc") == 0) { |
| 49 | return set_bootloader_message_block(in, v); |
Doug Zongker | 04611da | 2010-08-12 15:35:29 -0700 | [diff] [blame] | 50 | } |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 51 | LOGE("unknown misc partition fs_type \"%s\"\n", v->fs_type); |
| 52 | return -1; |
Doug Zongker | 04611da | 2010-08-12 15:35:29 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 55 | // ------------------------------ |
| 56 | // for misc partitions on MTD |
| 57 | // ------------------------------ |
Doug Zongker | 04611da | 2010-08-12 15:35:29 -0700 | [diff] [blame] | 58 | |
| 59 | static const int MISC_PAGES = 3; // number of pages to save |
| 60 | static const int MISC_COMMAND_PAGE = 1; // bootloader command is this page |
| 61 | |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 62 | static int get_bootloader_message_mtd(struct bootloader_message *out, |
| 63 | const Volume* v) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 64 | size_t write_size; |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 65 | mtd_scan_partitions(); |
| 66 | const MtdPartition *part = mtd_find_partition_by_name(v->device); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 67 | if (part == NULL || mtd_partition_info(part, NULL, NULL, &write_size)) { |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 68 | LOGE("Can't find %s\n", v->device); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 69 | return -1; |
| 70 | } |
| 71 | |
| 72 | MtdReadContext *read = mtd_read_partition(part); |
| 73 | if (read == NULL) { |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 74 | LOGE("Can't open %s\n(%s)\n", v->device, strerror(errno)); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 75 | return -1; |
| 76 | } |
| 77 | |
| 78 | const ssize_t size = write_size * MISC_PAGES; |
| 79 | char data[size]; |
| 80 | ssize_t r = mtd_read_data(read, data, size); |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 81 | if (r != size) LOGE("Can't read %s\n(%s)\n", v->device, strerror(errno)); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 82 | mtd_read_close(read); |
| 83 | if (r != size) return -1; |
| 84 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 85 | memcpy(out, &data[write_size * MISC_COMMAND_PAGE], sizeof(*out)); |
| 86 | return 0; |
| 87 | } |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 88 | static int set_bootloader_message_mtd(const struct bootloader_message *in, |
| 89 | const Volume* v) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 90 | size_t write_size; |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 91 | mtd_scan_partitions(); |
| 92 | const MtdPartition *part = mtd_find_partition_by_name(v->device); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 93 | if (part == NULL || mtd_partition_info(part, NULL, NULL, &write_size)) { |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 94 | LOGE("Can't find %s\n", v->device); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 95 | return -1; |
| 96 | } |
| 97 | |
| 98 | MtdReadContext *read = mtd_read_partition(part); |
| 99 | if (read == NULL) { |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 100 | LOGE("Can't open %s\n(%s)\n", v->device, strerror(errno)); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 101 | return -1; |
| 102 | } |
| 103 | |
| 104 | ssize_t size = write_size * MISC_PAGES; |
| 105 | char data[size]; |
| 106 | ssize_t r = mtd_read_data(read, data, size); |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 107 | if (r != size) LOGE("Can't read %s\n(%s)\n", v->device, strerror(errno)); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 108 | mtd_read_close(read); |
| 109 | if (r != size) return -1; |
| 110 | |
| 111 | memcpy(&data[write_size * MISC_COMMAND_PAGE], in, sizeof(*in)); |
| 112 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 113 | MtdWriteContext *write = mtd_write_partition(part); |
| 114 | if (write == NULL) { |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 115 | LOGE("Can't open %s\n(%s)\n", v->device, strerror(errno)); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 116 | return -1; |
| 117 | } |
| 118 | if (mtd_write_data(write, data, size) != size) { |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 119 | LOGE("Can't write %s\n(%s)\n", v->device, strerror(errno)); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 120 | mtd_write_close(write); |
| 121 | return -1; |
| 122 | } |
| 123 | if (mtd_write_close(write)) { |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 124 | LOGE("Can't finish %s\n(%s)\n", v->device, strerror(errno)); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 125 | return -1; |
| 126 | } |
| 127 | |
| 128 | LOGI("Set boot command \"%s\"\n", in->command[0] != 255 ? in->command : ""); |
| 129 | return 0; |
| 130 | } |
Doug Zongker | 04611da | 2010-08-12 15:35:29 -0700 | [diff] [blame] | 131 | |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 132 | |
| 133 | // ------------------------------------ |
| 134 | // for misc partitions on block devices |
| 135 | // ------------------------------------ |
| 136 | |
Doug Zongker | cfd256a | 2011-04-22 09:26:44 -0700 | [diff] [blame] | 137 | static void wait_for_device(const char* fn) { |
| 138 | int tries = 0; |
| 139 | int ret; |
| 140 | struct stat buf; |
| 141 | do { |
| 142 | ++tries; |
| 143 | ret = stat(fn, &buf); |
| 144 | if (ret) { |
| 145 | printf("stat %s try %d: %s\n", fn, tries, strerror(errno)); |
| 146 | sleep(1); |
| 147 | } |
| 148 | } while (ret && tries < 10); |
| 149 | if (ret) { |
| 150 | printf("failed to stat %s\n", fn); |
| 151 | } |
| 152 | } |
| 153 | |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 154 | static int get_bootloader_message_block(struct bootloader_message *out, |
| 155 | const Volume* v) { |
Doug Zongker | cfd256a | 2011-04-22 09:26:44 -0700 | [diff] [blame] | 156 | wait_for_device(v->device); |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 157 | FILE* f = fopen(v->device, "rb"); |
| 158 | if (f == NULL) { |
| 159 | LOGE("Can't open %s\n(%s)\n", v->device, strerror(errno)); |
| 160 | return -1; |
| 161 | } |
| 162 | struct bootloader_message temp; |
| 163 | int count = fread(&temp, sizeof(temp), 1, f); |
| 164 | if (count != 1) { |
| 165 | LOGE("Failed reading %s\n(%s)\n", v->device, strerror(errno)); |
| 166 | return -1; |
| 167 | } |
| 168 | if (fclose(f) != 0) { |
| 169 | LOGE("Failed closing %s\n(%s)\n", v->device, strerror(errno)); |
| 170 | return -1; |
| 171 | } |
| 172 | memcpy(out, &temp, sizeof(temp)); |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | static int set_bootloader_message_block(const struct bootloader_message *in, |
| 177 | const Volume* v) { |
Doug Zongker | cfd256a | 2011-04-22 09:26:44 -0700 | [diff] [blame] | 178 | wait_for_device(v->device); |
Doug Zongker | cc8cd3f | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 179 | FILE* f = fopen(v->device, "wb"); |
| 180 | if (f == NULL) { |
| 181 | LOGE("Can't open %s\n(%s)\n", v->device, strerror(errno)); |
| 182 | return -1; |
| 183 | } |
| 184 | int count = fwrite(in, sizeof(*in), 1, f); |
| 185 | if (count != 1) { |
| 186 | LOGE("Failed writing %s\n(%s)\n", v->device, strerror(errno)); |
| 187 | return -1; |
| 188 | } |
| 189 | if (fclose(f) != 0) { |
| 190 | LOGE("Failed closing %s\n(%s)\n", v->device, strerror(errno)); |
| 191 | return -1; |
| 192 | } |
| 193 | return 0; |
| 194 | } |