Dees_Troy | 7c2dec8 | 2012-09-26 09:49:14 -0400 | [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 | extern "C" { |
| 20 | #include "mtdutils/mtdutils.h" |
| 21 | } |
| 22 | |
| 23 | #include <errno.h> |
| 24 | #include <stdio.h> |
| 25 | #include <string.h> |
| 26 | #include <sys/stat.h> |
| 27 | #include <unistd.h> |
| 28 | #include "partitions.hpp" |
| 29 | |
| 30 | static int get_bootloader_message_mtd(struct bootloader_message *out, const TWPartition* Partition); |
| 31 | static int set_bootloader_message_mtd(const struct bootloader_message *in, const TWPartition* Partition); |
| 32 | static int get_bootloader_message_block(struct bootloader_message *out, const TWPartition* Partition); |
| 33 | static int set_bootloader_message_block(const struct bootloader_message *in, const TWPartition* Partition); |
| 34 | |
| 35 | int get_bootloader_message(struct bootloader_message *out) { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 36 | TWPartition* Part = PartitionManager.Find_Partition_By_Path("/misc"); |
| 37 | if (Part == NULL) { |
| 38 | //LOGE("Cannot load volume /misc!\n"); |
| 39 | return -1; |
| 40 | } |
| 41 | |
| 42 | if (Part->Current_File_System == "mtd") |
| 43 | return get_bootloader_message_mtd(out, Part); |
| 44 | else if (Part->Current_File_System == "emmc") |
| 45 | return get_bootloader_message_block(out, Part); |
| 46 | |
| 47 | LOGE("unknown misc partition fs_type \"%s\"\n", Part->Current_File_System.c_str()); |
| 48 | return -1; |
Dees_Troy | 7c2dec8 | 2012-09-26 09:49:14 -0400 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | int set_bootloader_message(const struct bootloader_message *in) { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 52 | TWPartition* Part = PartitionManager.Find_Partition_By_Path("/misc"); |
| 53 | if (Part == NULL) { |
| 54 | //LOGE("Cannot load volume /misc!\n"); |
| 55 | return -1; |
| 56 | } |
| 57 | |
| 58 | if (Part->Current_File_System == "mtd") |
| 59 | return set_bootloader_message_mtd(in, Part); |
| 60 | else if (Part->Current_File_System == "emmc") |
| 61 | return set_bootloader_message_block(in, Part); |
| 62 | |
| 63 | LOGE("unknown misc partition fs_type \"%s\"\n", Part->Current_File_System.c_str()); |
| 64 | return -1; |
Dees_Troy | 7c2dec8 | 2012-09-26 09:49:14 -0400 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | // ------------------------------ |
| 68 | // for misc partitions on MTD |
| 69 | // ------------------------------ |
| 70 | |
| 71 | static const int MISC_PAGES = 3; // number of pages to save |
| 72 | static const int MISC_COMMAND_PAGE = 1; // bootloader command is this page |
| 73 | |
| 74 | static int get_bootloader_message_mtd(struct bootloader_message *out, |
| 75 | const TWPartition* Partition) { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 76 | size_t write_size; |
| 77 | mtd_scan_partitions(); |
| 78 | const MtdPartition *part = mtd_find_partition_by_name(Partition->MTD_Name.c_str()); |
| 79 | if (part == NULL || mtd_partition_info(part, NULL, NULL, &write_size)) { |
| 80 | LOGE("Can't find %s\n", Partition->MTD_Name.c_str()); |
| 81 | return -1; |
| 82 | } |
Dees_Troy | 7c2dec8 | 2012-09-26 09:49:14 -0400 | [diff] [blame] | 83 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 84 | MtdReadContext *read = mtd_read_partition(part); |
| 85 | if (read == NULL) { |
| 86 | LOGE("Can't open %s\n(%s)\n", Partition->MTD_Name.c_str(), strerror(errno)); |
| 87 | return -1; |
| 88 | } |
Dees_Troy | 7c2dec8 | 2012-09-26 09:49:14 -0400 | [diff] [blame] | 89 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 90 | const ssize_t size = write_size * MISC_PAGES; |
| 91 | char data[size]; |
| 92 | ssize_t r = mtd_read_data(read, data, size); |
| 93 | if (r != size) LOGE("Can't read %s\n(%s)\n", Partition->MTD_Name.c_str(), strerror(errno)); |
| 94 | mtd_read_close(read); |
| 95 | if (r != size) return -1; |
Dees_Troy | 7c2dec8 | 2012-09-26 09:49:14 -0400 | [diff] [blame] | 96 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 97 | memcpy(out, &data[write_size * MISC_COMMAND_PAGE], sizeof(*out)); |
| 98 | return 0; |
Dees_Troy | 7c2dec8 | 2012-09-26 09:49:14 -0400 | [diff] [blame] | 99 | } |
| 100 | static int set_bootloader_message_mtd(const struct bootloader_message *in, |
| 101 | const TWPartition* Partition) { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 102 | size_t write_size; |
| 103 | mtd_scan_partitions(); |
| 104 | const MtdPartition *part = mtd_find_partition_by_name(Partition->MTD_Name.c_str()); |
| 105 | if (part == NULL || mtd_partition_info(part, NULL, NULL, &write_size)) { |
| 106 | LOGE("Can't find %s\n", Partition->MTD_Name.c_str()); |
| 107 | return -1; |
| 108 | } |
Dees_Troy | 7c2dec8 | 2012-09-26 09:49:14 -0400 | [diff] [blame] | 109 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 110 | MtdReadContext *read = mtd_read_partition(part); |
| 111 | if (read == NULL) { |
| 112 | LOGE("Can't open %s\n(%s)\n", Partition->MTD_Name.c_str(), strerror(errno)); |
| 113 | return -1; |
| 114 | } |
Dees_Troy | 7c2dec8 | 2012-09-26 09:49:14 -0400 | [diff] [blame] | 115 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 116 | ssize_t size = write_size * MISC_PAGES; |
| 117 | char data[size]; |
| 118 | ssize_t r = mtd_read_data(read, data, size); |
| 119 | if (r != size) LOGE("Can't read %s\n(%s)\n", Partition->MTD_Name.c_str(), strerror(errno)); |
| 120 | mtd_read_close(read); |
| 121 | if (r != size) return -1; |
Dees_Troy | 7c2dec8 | 2012-09-26 09:49:14 -0400 | [diff] [blame] | 122 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 123 | memcpy(&data[write_size * MISC_COMMAND_PAGE], in, sizeof(*in)); |
Dees_Troy | 7c2dec8 | 2012-09-26 09:49:14 -0400 | [diff] [blame] | 124 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 125 | MtdWriteContext *write = mtd_write_partition(part); |
| 126 | if (write == NULL) { |
| 127 | LOGE("Can't open %s\n(%s)\n", Partition->MTD_Name.c_str(), strerror(errno)); |
| 128 | return -1; |
| 129 | } |
| 130 | if (mtd_write_data(write, data, size) != size) { |
| 131 | LOGE("Can't write %s\n(%s)\n", Partition->MTD_Name.c_str(), strerror(errno)); |
| 132 | mtd_write_close(write); |
| 133 | return -1; |
| 134 | } |
| 135 | if (mtd_write_close(write)) { |
| 136 | LOGE("Can't finish %s\n(%s)\n", Partition->MTD_Name.c_str(), strerror(errno)); |
| 137 | return -1; |
| 138 | } |
Dees_Troy | 7c2dec8 | 2012-09-26 09:49:14 -0400 | [diff] [blame] | 139 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 140 | LOGI("Set boot command \"%s\"\n", in->command[0] != 255 ? in->command : ""); |
| 141 | return 0; |
Dees_Troy | 7c2dec8 | 2012-09-26 09:49:14 -0400 | [diff] [blame] | 142 | } |
| 143 | |
Dees_Troy | 7c2dec8 | 2012-09-26 09:49:14 -0400 | [diff] [blame] | 144 | // ------------------------------------ |
| 145 | // for misc partitions on block devices |
| 146 | // ------------------------------------ |
| 147 | |
| 148 | static void wait_for_device(const char* fn) { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 149 | int tries = 0; |
| 150 | int ret; |
| 151 | struct stat buf; |
| 152 | |
| 153 | do { |
| 154 | ++tries; |
| 155 | ret = stat(fn, &buf); |
| 156 | if (ret) { |
| 157 | printf("stat %s try %d: %s\n", fn, tries, strerror(errno)); |
| 158 | sleep(1); |
| 159 | } |
| 160 | } while (ret && tries < 10); |
| 161 | |
| 162 | if (ret) |
| 163 | printf("failed to stat %s\n", fn); |
Dees_Troy | 7c2dec8 | 2012-09-26 09:49:14 -0400 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | static int get_bootloader_message_block(struct bootloader_message *out, |
| 167 | const TWPartition* Partition) { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 168 | wait_for_device(Partition->Actual_Block_Device.c_str()); |
| 169 | |
| 170 | FILE* f = fopen(Partition->Actual_Block_Device.c_str(), "rb"); |
| 171 | if (f == NULL) { |
| 172 | LOGE("Can't open %s\n(%s)\n", Partition->Actual_Block_Device.c_str(), strerror(errno)); |
| 173 | return -1; |
| 174 | } |
| 175 | |
| 176 | struct bootloader_message temp; |
| 177 | int count = fread(&temp, sizeof(temp), 1, f); |
| 178 | if (count != 1) { |
| 179 | LOGE("Failed reading %s\n(%s)\n", Partition->Actual_Block_Device.c_str(), strerror(errno)); |
| 180 | return -1; |
| 181 | } |
| 182 | |
| 183 | if (fclose(f) != 0) { |
| 184 | LOGE("Failed closing %s\n(%s)\n", Partition->Actual_Block_Device.c_str(), strerror(errno)); |
| 185 | return -1; |
| 186 | } |
| 187 | |
| 188 | memcpy(out, &temp, sizeof(temp)); |
| 189 | return 0; |
Dees_Troy | 7c2dec8 | 2012-09-26 09:49:14 -0400 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | static int set_bootloader_message_block(const struct bootloader_message *in, |
| 193 | const TWPartition* Partition) { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 194 | wait_for_device(Partition->Actual_Block_Device.c_str()); |
| 195 | |
| 196 | FILE* f = fopen(Partition->Actual_Block_Device.c_str(), "wb"); |
| 197 | if (f == NULL) { |
| 198 | LOGE("Can't open %s\n(%s)\n", Partition->Actual_Block_Device.c_str(), strerror(errno)); |
| 199 | return -1; |
| 200 | } |
| 201 | |
| 202 | int count = fwrite(in, sizeof(*in), 1, f); |
| 203 | if (count != 1) { |
| 204 | LOGE("Failed writing %s\n(%s)\n", Partition->Actual_Block_Device.c_str(), strerror(errno)); |
| 205 | return -1; |
| 206 | } |
| 207 | |
| 208 | if (fclose(f) != 0) { |
| 209 | LOGE("Failed closing %s\n(%s)\n", Partition->Actual_Block_Device.c_str(), strerror(errno)); |
| 210 | return -1; |
| 211 | } |
| 212 | return 0; |
Dees_Troy | 7c2dec8 | 2012-09-26 09:49:14 -0400 | [diff] [blame] | 213 | } |