blob: 1c209798332cd082a5824d9ea9c00eac21cdfa30 [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>
Tao Bao8559bbf2016-02-18 17:09:10 -080024#include <fcntl.h>
25#include <inttypes.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080026#include <stdio.h>
27#include <string.h>
Doug Zongkercfd256a2011-04-22 09:26:44 -070028#include <sys/stat.h>
Tao Bao8559bbf2016-02-18 17:09:10 -080029#include <sys/types.h>
Doug Zongkercfd256a2011-04-22 09:26:44 -070030#include <unistd.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080031
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070032static int get_bootloader_message_mtd(struct bootloader_message *out, const Volume* v);
33static int set_bootloader_message_mtd(const struct bootloader_message *in, const Volume* v);
34static int get_bootloader_message_block(struct bootloader_message *out, const Volume* v);
35static int set_bootloader_message_block(const struct bootloader_message *in, const Volume* v);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080036
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070037int get_bootloader_message(struct bootloader_message *out) {
38 Volume* v = volume_for_path("/misc");
Adam Blissb2ceb692011-07-13 15:13:54 -070039 if (v == NULL) {
40 LOGE("Cannot load volume /misc!\n");
41 return -1;
42 }
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070043 if (strcmp(v->fs_type, "mtd") == 0) {
44 return get_bootloader_message_mtd(out, v);
45 } else if (strcmp(v->fs_type, "emmc") == 0) {
46 return get_bootloader_message_block(out, v);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080047 }
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070048 LOGE("unknown misc partition fs_type \"%s\"\n", v->fs_type);
49 return -1;
Doug Zongker04611da2010-08-12 15:35:29 -070050}
51
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070052int set_bootloader_message(const struct bootloader_message *in) {
53 Volume* v = volume_for_path("/misc");
Adam Blissb2ceb692011-07-13 15:13:54 -070054 if (v == NULL) {
55 LOGE("Cannot load volume /misc!\n");
56 return -1;
57 }
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070058 if (strcmp(v->fs_type, "mtd") == 0) {
59 return set_bootloader_message_mtd(in, v);
60 } else if (strcmp(v->fs_type, "emmc") == 0) {
61 return set_bootloader_message_block(in, v);
Doug Zongker04611da2010-08-12 15:35:29 -070062 }
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070063 LOGE("unknown misc partition fs_type \"%s\"\n", v->fs_type);
64 return -1;
Doug Zongker04611da2010-08-12 15:35:29 -070065}
66
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070067// ------------------------------
68// for misc partitions on MTD
69// ------------------------------
Doug Zongker04611da2010-08-12 15:35:29 -070070
71static const int MISC_PAGES = 3; // number of pages to save
72static const int MISC_COMMAND_PAGE = 1; // bootloader command is this page
73
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070074static int get_bootloader_message_mtd(struct bootloader_message *out,
75 const Volume* v) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080076 size_t write_size;
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070077 mtd_scan_partitions();
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080078 const MtdPartition *part = mtd_find_partition_by_name(v->blk_device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080079 if (part == NULL || mtd_partition_info(part, NULL, NULL, &write_size)) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080080 LOGE("Can't find %s\n", v->blk_device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080081 return -1;
82 }
83
84 MtdReadContext *read = mtd_read_partition(part);
85 if (read == NULL) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080086 LOGE("Can't open %s\n(%s)\n", v->blk_device, strerror(errno));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080087 return -1;
88 }
89
90 const ssize_t size = write_size * MISC_PAGES;
91 char data[size];
92 ssize_t r = mtd_read_data(read, data, size);
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080093 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 -080094 mtd_read_close(read);
95 if (r != size) return -1;
96
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080097 memcpy(out, &data[write_size * MISC_COMMAND_PAGE], sizeof(*out));
98 return 0;
99}
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700100static int set_bootloader_message_mtd(const struct bootloader_message *in,
101 const Volume* v) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800102 size_t write_size;
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700103 mtd_scan_partitions();
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800104 const MtdPartition *part = mtd_find_partition_by_name(v->blk_device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800105 if (part == NULL || mtd_partition_info(part, NULL, NULL, &write_size)) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800106 LOGE("Can't find %s\n", v->blk_device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800107 return -1;
108 }
109
110 MtdReadContext *read = mtd_read_partition(part);
111 if (read == NULL) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800112 LOGE("Can't open %s\n(%s)\n", v->blk_device, strerror(errno));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800113 return -1;
114 }
115
116 ssize_t size = write_size * MISC_PAGES;
117 char data[size];
118 ssize_t r = mtd_read_data(read, data, size);
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800119 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 -0800120 mtd_read_close(read);
121 if (r != size) return -1;
122
123 memcpy(&data[write_size * MISC_COMMAND_PAGE], in, sizeof(*in));
124
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800125 MtdWriteContext *write = mtd_write_partition(part);
126 if (write == NULL) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800127 LOGE("Can't open %s\n(%s)\n", v->blk_device, strerror(errno));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800128 return -1;
129 }
130 if (mtd_write_data(write, data, size) != size) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800131 LOGE("Can't write %s\n(%s)\n", v->blk_device, strerror(errno));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800132 mtd_write_close(write);
133 return -1;
134 }
135 if (mtd_write_close(write)) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800136 LOGE("Can't finish %s\n(%s)\n", v->blk_device, strerror(errno));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800137 return -1;
138 }
139
140 LOGI("Set boot command \"%s\"\n", in->command[0] != 255 ? in->command : "");
141 return 0;
142}
Doug Zongker04611da2010-08-12 15:35:29 -0700143
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700144
145// ------------------------------------
146// for misc partitions on block devices
147// ------------------------------------
148
Doug Zongkercfd256a2011-04-22 09:26:44 -0700149static void wait_for_device(const char* fn) {
150 int tries = 0;
151 int ret;
152 struct stat buf;
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 if (ret) {
162 printf("failed to stat %s\n", fn);
163 }
164}
165
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700166static int get_bootloader_message_block(struct bootloader_message *out,
167 const Volume* v) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800168 wait_for_device(v->blk_device);
169 FILE* f = fopen(v->blk_device, "rb");
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700170 if (f == NULL) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800171 LOGE("Can't open %s\n(%s)\n", v->blk_device, strerror(errno));
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700172 return -1;
173 }
174 struct bootloader_message temp;
175 int count = fread(&temp, sizeof(temp), 1, f);
176 if (count != 1) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800177 LOGE("Failed reading %s\n(%s)\n", v->blk_device, strerror(errno));
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700178 return -1;
179 }
180 if (fclose(f) != 0) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800181 LOGE("Failed closing %s\n(%s)\n", v->blk_device, strerror(errno));
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700182 return -1;
183 }
184 memcpy(out, &temp, sizeof(temp));
185 return 0;
186}
187
188static int set_bootloader_message_block(const struct bootloader_message *in,
189 const Volume* v) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800190 wait_for_device(v->blk_device);
Tao Bao8559bbf2016-02-18 17:09:10 -0800191 int fd = open(v->blk_device, O_WRONLY | O_SYNC);
192 if (fd == -1) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800193 LOGE("Can't open %s\n(%s)\n", v->blk_device, strerror(errno));
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700194 return -1;
195 }
Tao Bao8559bbf2016-02-18 17:09:10 -0800196 size_t written = 0;
197 const uint8_t* start = reinterpret_cast<const uint8_t*>(in);
198 size_t total = sizeof(*in);
199 while (written < total) {
200 ssize_t wrote = TEMP_FAILURE_RETRY(write(fd, start + written, total - written));
201 if (wrote == -1) {
202 LOGE("failed to write %" PRId64 " bytes: %s\n",
203 static_cast<off64_t>(written), strerror(errno));
204 return -1;
205 }
206 written += wrote;
207 }
208
209 if (fsync(fd) == -1) {
210 LOGE("failed to fsync \"%s\": %s\n", v->blk_device, strerror(errno));
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700211 return -1;
212 }
Tao Bao8559bbf2016-02-18 17:09:10 -0800213 if (close(fd) == -1) {
214 LOGE("failed to close %s: %s\n", v->blk_device, strerror(errno));
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700215 return -1;
216 }
217 return 0;
218}