blob: e88160d8cbcda33be61465ff2b1224480d5f3873 [file] [log] [blame]
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001/*
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 Zongkercfd256a2011-04-22 09:26:44 -070025#include <sys/stat.h>
26#include <unistd.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080027
Doug Zongkerd4208f92010-09-20 12:16:13 -070028static int get_bootloader_message_mtd(struct bootloader_message *out, const Volume* v);
29static int set_bootloader_message_mtd(const struct bootloader_message *in, const Volume* v);
30static int get_bootloader_message_block(struct bootloader_message *out, const Volume* v);
31static int set_bootloader_message_block(const struct bootloader_message *in, const Volume* v);
32
33int get_bootloader_message(struct bootloader_message *out) {
34 Volume* v = volume_for_path("/misc");
Kolja Dummann75017982010-12-28 23:54:29 +010035 if(v)
36 {
37 if (strcmp(v->fs_type, "mtd") == 0) {
38 return get_bootloader_message_mtd(out, v);
39 } else if (strcmp(v->fs_type, "emmc") == 0) {
40 return get_bootloader_message_block(out, v);
41 }
42 LOGE("unknown misc partition fs_type \"%s\"\n", v->fs_type);
43 return -1;
Doug Zongkerd4208f92010-09-20 12:16:13 -070044 }
Doug Zongkerd4208f92010-09-20 12:16:13 -070045 return -1;
46}
47
48int set_bootloader_message(const struct bootloader_message *in) {
49 Volume* v = volume_for_path("/misc");
Kolja Dummann75017982010-12-28 23:54:29 +010050 if(v)
51 {
52 if (strcmp(v->fs_type, "mtd") == 0) {
53 return set_bootloader_message_mtd(in, v);
54 } else if (strcmp(v->fs_type, "emmc") == 0) {
55 return set_bootloader_message_block(in, v);
56 }
57 LOGE("unknown misc partition fs_type \"%s\"\n", v->fs_type);
58 return -1;
Doug Zongkerd4208f92010-09-20 12:16:13 -070059 }
Doug Zongkerd4208f92010-09-20 12:16:13 -070060 return -1;
61}
62
63// ------------------------------
64// for misc partitions on MTD
65// ------------------------------
66
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080067static const int MISC_PAGES = 3; // number of pages to save
68static const int MISC_COMMAND_PAGE = 1; // bootloader command is this page
69
Doug Zongkerd4208f92010-09-20 12:16:13 -070070static int get_bootloader_message_mtd(struct bootloader_message *out,
71 const Volume* v) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080072 size_t write_size;
Doug Zongkerd4208f92010-09-20 12:16:13 -070073 mtd_scan_partitions();
74 const MtdPartition *part = mtd_find_partition_by_name(v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080075 if (part == NULL || mtd_partition_info(part, NULL, NULL, &write_size)) {
Doug Zongkerd4208f92010-09-20 12:16:13 -070076 LOGE("Can't find %s\n", v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080077 return -1;
78 }
79
80 MtdReadContext *read = mtd_read_partition(part);
81 if (read == NULL) {
Doug Zongkerd4208f92010-09-20 12:16:13 -070082 LOGE("Can't open %s\n(%s)\n", v->device, strerror(errno));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080083 return -1;
84 }
85
86 const ssize_t size = write_size * MISC_PAGES;
87 char data[size];
88 ssize_t r = mtd_read_data(read, data, size);
Doug Zongkerd4208f92010-09-20 12:16:13 -070089 if (r != size) LOGE("Can't read %s\n(%s)\n", v->device, strerror(errno));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080090 mtd_read_close(read);
91 if (r != size) return -1;
92
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080093 memcpy(out, &data[write_size * MISC_COMMAND_PAGE], sizeof(*out));
94 return 0;
95}
Doug Zongkerd4208f92010-09-20 12:16:13 -070096static int set_bootloader_message_mtd(const struct bootloader_message *in,
97 const Volume* v) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080098 size_t write_size;
Doug Zongkerd4208f92010-09-20 12:16:13 -070099 mtd_scan_partitions();
100 const MtdPartition *part = mtd_find_partition_by_name(v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800101 if (part == NULL || mtd_partition_info(part, NULL, NULL, &write_size)) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700102 LOGE("Can't find %s\n", v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800103 return -1;
104 }
105
106 MtdReadContext *read = mtd_read_partition(part);
107 if (read == NULL) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700108 LOGE("Can't open %s\n(%s)\n", v->device, strerror(errno));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800109 return -1;
110 }
111
112 ssize_t size = write_size * MISC_PAGES;
113 char data[size];
114 ssize_t r = mtd_read_data(read, data, size);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700115 if (r != size) LOGE("Can't read %s\n(%s)\n", v->device, strerror(errno));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800116 mtd_read_close(read);
117 if (r != size) return -1;
118
119 memcpy(&data[write_size * MISC_COMMAND_PAGE], in, sizeof(*in));
120
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800121 MtdWriteContext *write = mtd_write_partition(part);
122 if (write == NULL) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700123 LOGE("Can't open %s\n(%s)\n", v->device, strerror(errno));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800124 return -1;
125 }
126 if (mtd_write_data(write, data, size) != size) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700127 LOGE("Can't write %s\n(%s)\n", v->device, strerror(errno));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800128 mtd_write_close(write);
129 return -1;
130 }
131 if (mtd_write_close(write)) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700132 LOGE("Can't finish %s\n(%s)\n", v->device, strerror(errno));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800133 return -1;
134 }
135
136 LOGI("Set boot command \"%s\"\n", in->command[0] != 255 ? in->command : "");
137 return 0;
138}
Doug Zongkerd4208f92010-09-20 12:16:13 -0700139
140
141// ------------------------------------
142// for misc partitions on block devices
143// ------------------------------------
144
Doug Zongkercfd256a2011-04-22 09:26:44 -0700145static void wait_for_device(const char* fn) {
146 int tries = 0;
147 int ret;
148 struct stat buf;
149 do {
150 ++tries;
151 ret = stat(fn, &buf);
152 if (ret) {
153 printf("stat %s try %d: %s\n", fn, tries, strerror(errno));
154 sleep(1);
155 }
156 } while (ret && tries < 10);
157 if (ret) {
158 printf("failed to stat %s\n", fn);
159 }
160}
161
Doug Zongkerd4208f92010-09-20 12:16:13 -0700162static int get_bootloader_message_block(struct bootloader_message *out,
163 const Volume* v) {
Doug Zongkercfd256a2011-04-22 09:26:44 -0700164 wait_for_device(v->device);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700165 FILE* f = fopen(v->device, "rb");
166 if (f == NULL) {
167 LOGE("Can't open %s\n(%s)\n", v->device, strerror(errno));
168 return -1;
169 }
170 struct bootloader_message temp;
171 int count = fread(&temp, sizeof(temp), 1, f);
172 if (count != 1) {
173 LOGE("Failed reading %s\n(%s)\n", v->device, strerror(errno));
174 return -1;
175 }
176 if (fclose(f) != 0) {
177 LOGE("Failed closing %s\n(%s)\n", v->device, strerror(errno));
178 return -1;
179 }
180 memcpy(out, &temp, sizeof(temp));
181 return 0;
182}
183
184static int set_bootloader_message_block(const struct bootloader_message *in,
185 const Volume* v) {
Doug Zongkercfd256a2011-04-22 09:26:44 -0700186 wait_for_device(v->device);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700187 FILE* f = fopen(v->device, "wb");
188 if (f == NULL) {
189 LOGE("Can't open %s\n(%s)\n", v->device, strerror(errno));
190 return -1;
191 }
192 int count = fwrite(in, sizeof(*in), 1, f);
193 if (count != 1) {
194 LOGE("Failed writing %s\n(%s)\n", v->device, strerror(errno));
195 return -1;
196 }
197 if (fclose(f) != 0) {
198 LOGE("Failed closing %s\n(%s)\n", v->device, strerror(errno));
199 return -1;
200 }
201 return 0;
202}
Koushik Dutta7f13e152011-09-08 16:55:35 -0700203
204/* Update Image
205 *
206 * - will be stored in the "cache" partition
207 * - bad blocks will be ignored, like boot.img and recovery.img
208 * - the first block will be the image header (described below)
209 * - the size is in BYTES, inclusive of the header
210 * - offsets are in BYTES from the start of the update header
211 * - two raw bitmaps will be included, the "busy" and "fail" bitmaps
212 * - for dream, the bitmaps will be 320x480x16bpp RGB565
213 */
214
215#define UPDATE_MAGIC "MSM-RADIO-UPDATE"
216#define UPDATE_MAGIC_SIZE 16
217#define UPDATE_VERSION 0x00010000
218
219struct update_header {
220 unsigned char MAGIC[UPDATE_MAGIC_SIZE];
221
222 unsigned version;
223 unsigned size;
224
225 unsigned image_offset;
226 unsigned image_length;
227
228 unsigned bitmap_width;
229 unsigned bitmap_height;
230 unsigned bitmap_bpp;
231
232 unsigned busy_bitmap_offset;
233 unsigned busy_bitmap_length;
234
235 unsigned fail_bitmap_offset;
236 unsigned fail_bitmap_length;
237};
238
239int write_update_for_bootloader(
240 const char *update, int update_length,
241 int bitmap_width, int bitmap_height, int bitmap_bpp,
242 const char *busy_bitmap, const char *fail_bitmap) {
243 if (ensure_path_unmounted("/cache")) {
244 LOGE("Can't unmount /cache\n");
245 return -1;
246 }
247
248 const MtdPartition *part = mtd_find_partition_by_name("cache");
249 if (part == NULL) {
250 LOGE("Can't find cache\n");
251 return -1;
252 }
253
254 MtdWriteContext *write = mtd_write_partition(part);
255 if (write == NULL) {
256 LOGE("Can't open cache\n(%s)\n", strerror(errno));
257 return -1;
258 }
259
260 /* Write an invalid (zero) header first, to disable any previous
261 * update and any other structured contents (like a filesystem),
262 * and as a placeholder for the amount of space required.
263 */
264
265 struct update_header header;
266 memset(&header, 0, sizeof(header));
267 const ssize_t header_size = sizeof(header);
268 if (mtd_write_data(write, (char*) &header, header_size) != header_size) {
269 LOGE("Can't write header to cache\n(%s)\n", strerror(errno));
270 mtd_write_close(write);
271 return -1;
272 }
273
274 /* Write each section individually block-aligned, so we can write
275 * each block independently without complicated buffering.
276 */
277
278 memcpy(&header.MAGIC, UPDATE_MAGIC, UPDATE_MAGIC_SIZE);
279 header.version = UPDATE_VERSION;
280 header.size = header_size;
281
282 off_t image_start_pos = mtd_erase_blocks(write, 0);
283 header.image_length = update_length;
284 if ((int) header.image_offset == -1 ||
285 mtd_write_data(write, update, update_length) != update_length) {
286 LOGE("Can't write update to cache\n(%s)\n", strerror(errno));
287 mtd_write_close(write);
288 return -1;
289 }
290 off_t busy_start_pos = mtd_erase_blocks(write, 0);
291 header.image_offset = mtd_find_write_start(write, image_start_pos);
292
293 header.bitmap_width = bitmap_width;
294 header.bitmap_height = bitmap_height;
295 header.bitmap_bpp = bitmap_bpp;
296
297 int bitmap_length = (bitmap_bpp + 7) / 8 * bitmap_width * bitmap_height;
298
299 header.busy_bitmap_length = busy_bitmap != NULL ? bitmap_length : 0;
300 if ((int) header.busy_bitmap_offset == -1 ||
301 mtd_write_data(write, busy_bitmap, bitmap_length) != bitmap_length) {
302 LOGE("Can't write bitmap to cache\n(%s)\n", strerror(errno));
303 mtd_write_close(write);
304 return -1;
305 }
306 off_t fail_start_pos = mtd_erase_blocks(write, 0);
307 header.busy_bitmap_offset = mtd_find_write_start(write, busy_start_pos);
308
309 header.fail_bitmap_length = fail_bitmap != NULL ? bitmap_length : 0;
310 if ((int) header.fail_bitmap_offset == -1 ||
311 mtd_write_data(write, fail_bitmap, bitmap_length) != bitmap_length) {
312 LOGE("Can't write bitmap to cache\n(%s)\n", strerror(errno));
313 mtd_write_close(write);
314 return -1;
315 }
316 mtd_erase_blocks(write, 0);
317 header.fail_bitmap_offset = mtd_find_write_start(write, fail_start_pos);
318
319 /* Write the header last, after all the blocks it refers to, so that
320 * when the magic number is installed everything is valid.
321 */
322
323 if (mtd_write_close(write)) {
324 LOGE("Can't finish writing cache\n(%s)\n", strerror(errno));
325 return -1;
326 }
327
328 write = mtd_write_partition(part);
329 if (write == NULL) {
330 LOGE("Can't reopen cache\n(%s)\n", strerror(errno));
331 return -1;
332 }
333
334 if (mtd_write_data(write, (char*) &header, header_size) != header_size) {
335 LOGE("Can't rewrite header to cache\n(%s)\n", strerror(errno));
336 mtd_write_close(write);
337 return -1;
338 }
339
340 if (mtd_erase_blocks(write, 0) != image_start_pos) {
341 LOGE("Misalignment rewriting cache\n(%s)\n", strerror(errno));
342 mtd_write_close(write);
343 return -1;
344 }
345
346 if (mtd_write_close(write)) {
347 LOGE("Can't finish header of cache\n(%s)\n", strerror(errno));
348 return -1;
349 }
350
351 return 0;
Koushik Dutta0df56f42011-11-16 16:00:35 -0800352}