blob: 600d238f5f82662e6d75584772b31b66c3add312 [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
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080017#include <fs_mgr.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080018#include "bootloader.h"
19#include "common.h"
20#include "mtdutils/mtdutils.h"
21#include "roots.h"
22
23#include <errno.h>
24#include <stdio.h>
25#include <string.h>
Doug Zongkercfd256a2011-04-22 09:26:44 -070026#include <sys/stat.h>
27#include <unistd.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080028
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070029static int get_bootloader_message_mtd(struct bootloader_message *out, const Volume* v);
30static int set_bootloader_message_mtd(const struct bootloader_message *in, const Volume* v);
31static int get_bootloader_message_block(struct bootloader_message *out, const Volume* v);
32static int set_bootloader_message_block(const struct bootloader_message *in, const Volume* v);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080033
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070034int get_bootloader_message(struct bootloader_message *out) {
35 Volume* v = volume_for_path("/misc");
Adam Blissb2ceb692011-07-13 15:13:54 -070036 if (v == NULL) {
37 LOGE("Cannot load volume /misc!\n");
38 return -1;
39 }
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070040 if (strcmp(v->fs_type, "mtd") == 0) {
41 return get_bootloader_message_mtd(out, v);
42 } else if (strcmp(v->fs_type, "emmc") == 0) {
43 return get_bootloader_message_block(out, v);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080044 }
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070045 LOGE("unknown misc partition fs_type \"%s\"\n", v->fs_type);
46 return -1;
Doug Zongker04611da2010-08-12 15:35:29 -070047}
48
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070049int set_bootloader_message(const struct bootloader_message *in) {
50 Volume* v = volume_for_path("/misc");
Adam Blissb2ceb692011-07-13 15:13:54 -070051 if (v == NULL) {
52 LOGE("Cannot load volume /misc!\n");
53 return -1;
54 }
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070055 if (strcmp(v->fs_type, "mtd") == 0) {
56 return set_bootloader_message_mtd(in, v);
57 } else if (strcmp(v->fs_type, "emmc") == 0) {
58 return set_bootloader_message_block(in, v);
Doug Zongker04611da2010-08-12 15:35:29 -070059 }
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070060 LOGE("unknown misc partition fs_type \"%s\"\n", v->fs_type);
61 return -1;
Doug Zongker04611da2010-08-12 15:35:29 -070062}
63
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070064// ------------------------------
65// for misc partitions on MTD
66// ------------------------------
Doug Zongker04611da2010-08-12 15:35:29 -070067
68static const int MISC_PAGES = 3; // number of pages to save
69static const int MISC_COMMAND_PAGE = 1; // bootloader command is this page
70
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070071static int get_bootloader_message_mtd(struct bootloader_message *out,
72 const Volume* v) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080073 size_t write_size;
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070074 mtd_scan_partitions();
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080075 const MtdPartition *part = mtd_find_partition_by_name(v->blk_device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080076 if (part == NULL || mtd_partition_info(part, NULL, NULL, &write_size)) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080077 LOGE("Can't find %s\n", v->blk_device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080078 return -1;
79 }
80
81 MtdReadContext *read = mtd_read_partition(part);
82 if (read == NULL) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080083 LOGE("Can't open %s\n(%s)\n", v->blk_device, strerror(errno));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080084 return -1;
85 }
86
87 const ssize_t size = write_size * MISC_PAGES;
88 char data[size];
89 ssize_t r = mtd_read_data(read, data, size);
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080090 if (r != size) LOGE("Can't read %s\n(%s)\n", v->blk_device, strerror(errno));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080091 mtd_read_close(read);
92 if (r != size) return -1;
93
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080094 memcpy(out, &data[write_size * MISC_COMMAND_PAGE], sizeof(*out));
95 return 0;
96}
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070097static int set_bootloader_message_mtd(const struct bootloader_message *in,
98 const Volume* v) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080099 size_t write_size;
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700100 mtd_scan_partitions();
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800101 const MtdPartition *part = mtd_find_partition_by_name(v->blk_device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800102 if (part == NULL || mtd_partition_info(part, NULL, NULL, &write_size)) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800103 LOGE("Can't find %s\n", v->blk_device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800104 return -1;
105 }
106
107 MtdReadContext *read = mtd_read_partition(part);
108 if (read == NULL) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800109 LOGE("Can't open %s\n(%s)\n", v->blk_device, strerror(errno));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800110 return -1;
111 }
112
113 ssize_t size = write_size * MISC_PAGES;
114 char data[size];
115 ssize_t r = mtd_read_data(read, data, size);
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800116 if (r != size) LOGE("Can't read %s\n(%s)\n", v->blk_device, strerror(errno));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800117 mtd_read_close(read);
118 if (r != size) return -1;
119
120 memcpy(&data[write_size * MISC_COMMAND_PAGE], in, sizeof(*in));
121
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800122 MtdWriteContext *write = mtd_write_partition(part);
123 if (write == NULL) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800124 LOGE("Can't open %s\n(%s)\n", v->blk_device, strerror(errno));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800125 return -1;
126 }
127 if (mtd_write_data(write, data, size) != size) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800128 LOGE("Can't write %s\n(%s)\n", v->blk_device, strerror(errno));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800129 mtd_write_close(write);
130 return -1;
131 }
132 if (mtd_write_close(write)) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800133 LOGE("Can't finish %s\n(%s)\n", v->blk_device, strerror(errno));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800134 return -1;
135 }
136
137 LOGI("Set boot command \"%s\"\n", in->command[0] != 255 ? in->command : "");
138 return 0;
139}
Doug Zongker04611da2010-08-12 15:35:29 -0700140
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700141
142// ------------------------------------
143// for misc partitions on block devices
144// ------------------------------------
145
Doug Zongkercfd256a2011-04-22 09:26:44 -0700146static void wait_for_device(const char* fn) {
147 int tries = 0;
148 int ret;
149 struct stat buf;
150 do {
151 ++tries;
152 ret = stat(fn, &buf);
153 if (ret) {
154 printf("stat %s try %d: %s\n", fn, tries, strerror(errno));
155 sleep(1);
156 }
157 } while (ret && tries < 10);
158 if (ret) {
159 printf("failed to stat %s\n", fn);
160 }
161}
162
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700163static int get_bootloader_message_block(struct bootloader_message *out,
164 const Volume* v) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800165 wait_for_device(v->blk_device);
166 FILE* f = fopen(v->blk_device, "rb");
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700167 if (f == NULL) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800168 LOGE("Can't open %s\n(%s)\n", v->blk_device, strerror(errno));
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700169 return -1;
170 }
171 struct bootloader_message temp;
172 int count = fread(&temp, sizeof(temp), 1, f);
173 if (count != 1) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800174 LOGE("Failed reading %s\n(%s)\n", v->blk_device, strerror(errno));
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700175 return -1;
176 }
177 if (fclose(f) != 0) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800178 LOGE("Failed closing %s\n(%s)\n", v->blk_device, strerror(errno));
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700179 return -1;
180 }
181 memcpy(out, &temp, sizeof(temp));
182 return 0;
183}
184
185static int set_bootloader_message_block(const struct bootloader_message *in,
186 const Volume* v) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800187 wait_for_device(v->blk_device);
188 FILE* f = fopen(v->blk_device, "wb");
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700189 if (f == NULL) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800190 LOGE("Can't open %s\n(%s)\n", v->blk_device, strerror(errno));
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700191 return -1;
192 }
193 int count = fwrite(in, sizeof(*in), 1, f);
194 if (count != 1) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800195 LOGE("Failed writing %s\n(%s)\n", v->blk_device, strerror(errno));
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700196 return -1;
197 }
198 if (fclose(f) != 0) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800199 LOGE("Failed closing %s\n(%s)\n", v->blk_device, strerror(errno));
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700200 return -1;
201 }
202 return 0;
203}