blob: a32f8b4c6cf37e05cd3516819f76bbcbcba736a7 [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
Tao Baobd82b272016-01-21 13:49:03 -080017#include <errno.h>
18#include <fcntl.h>
19#include <inttypes.h>
20#include <stdio.h>
21#include <string.h>
22#include <sys/stat.h>
23#include <sys/types.h>
24#include <unistd.h>
25
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080026#include <fs_mgr.h>
Tao Baobd82b272016-01-21 13:49:03 -080027
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080028#include "bootloader.h"
29#include "common.h"
30#include "mtdutils/mtdutils.h"
31#include "roots.h"
Elliott Hughesbcabd092016-03-22 20:19:22 -070032#include <android-base/unique_fd.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080033
Tao Baobd82b272016-01-21 13:49:03 -080034static int get_bootloader_message_mtd(bootloader_message* out, const Volume* v);
35static int set_bootloader_message_mtd(const bootloader_message* in, const Volume* v);
36static int get_bootloader_message_block(bootloader_message* out, const Volume* v);
37static int set_bootloader_message_block(const bootloader_message* in, const Volume* v);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080038
Tao Baobd82b272016-01-21 13:49:03 -080039int get_bootloader_message(bootloader_message* out) {
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070040 Volume* v = volume_for_path("/misc");
Tao Baobd82b272016-01-21 13:49:03 -080041 if (v == nullptr) {
42 LOGE("Cannot load volume /misc!\n");
43 return -1;
Adam Blissb2ceb692011-07-13 15:13:54 -070044 }
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070045 if (strcmp(v->fs_type, "mtd") == 0) {
46 return get_bootloader_message_mtd(out, v);
47 } else if (strcmp(v->fs_type, "emmc") == 0) {
48 return get_bootloader_message_block(out, v);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080049 }
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070050 LOGE("unknown misc partition fs_type \"%s\"\n", v->fs_type);
51 return -1;
Doug Zongker04611da2010-08-12 15:35:29 -070052}
53
Tao Baobd82b272016-01-21 13:49:03 -080054int set_bootloader_message(const bootloader_message* in) {
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070055 Volume* v = volume_for_path("/misc");
Tao Baobd82b272016-01-21 13:49:03 -080056 if (v == nullptr) {
57 LOGE("Cannot load volume /misc!\n");
58 return -1;
Adam Blissb2ceb692011-07-13 15:13:54 -070059 }
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070060 if (strcmp(v->fs_type, "mtd") == 0) {
61 return set_bootloader_message_mtd(in, v);
62 } else if (strcmp(v->fs_type, "emmc") == 0) {
63 return set_bootloader_message_block(in, v);
Doug Zongker04611da2010-08-12 15:35:29 -070064 }
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070065 LOGE("unknown misc partition fs_type \"%s\"\n", v->fs_type);
66 return -1;
Doug Zongker04611da2010-08-12 15:35:29 -070067}
68
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070069// ------------------------------
70// for misc partitions on MTD
71// ------------------------------
Doug Zongker04611da2010-08-12 15:35:29 -070072
73static const int MISC_PAGES = 3; // number of pages to save
74static const int MISC_COMMAND_PAGE = 1; // bootloader command is this page
75
Tao Baobd82b272016-01-21 13:49:03 -080076static int get_bootloader_message_mtd(bootloader_message* out,
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070077 const Volume* v) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080078 size_t write_size;
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070079 mtd_scan_partitions();
Tao Baobd82b272016-01-21 13:49:03 -080080 const MtdPartition* part = mtd_find_partition_by_name(v->blk_device);
81 if (part == nullptr || mtd_partition_info(part, nullptr, nullptr, &write_size)) {
82 LOGE("failed to find \"%s\"\n", v->blk_device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080083 return -1;
84 }
85
Tao Baobd82b272016-01-21 13:49:03 -080086 MtdReadContext* read = mtd_read_partition(part);
87 if (read == nullptr) {
88 LOGE("failed to open \"%s\": %s\n", v->blk_device, strerror(errno));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080089 return -1;
90 }
91
92 const ssize_t size = write_size * MISC_PAGES;
93 char data[size];
94 ssize_t r = mtd_read_data(read, data, size);
Tao Baobd82b272016-01-21 13:49:03 -080095 if (r != size) LOGE("failed to read \"%s\": %s\n", v->blk_device, strerror(errno));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080096 mtd_read_close(read);
97 if (r != size) return -1;
98
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080099 memcpy(out, &data[write_size * MISC_COMMAND_PAGE], sizeof(*out));
100 return 0;
101}
Tao Baobd82b272016-01-21 13:49:03 -0800102static int set_bootloader_message_mtd(const bootloader_message* in,
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700103 const Volume* v) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800104 size_t write_size;
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700105 mtd_scan_partitions();
Tao Baobd82b272016-01-21 13:49:03 -0800106 const MtdPartition* part = mtd_find_partition_by_name(v->blk_device);
107 if (part == nullptr || mtd_partition_info(part, nullptr, nullptr, &write_size)) {
108 LOGE("failed to find \"%s\"\n", v->blk_device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800109 return -1;
110 }
111
Tao Baobd82b272016-01-21 13:49:03 -0800112 MtdReadContext* read = mtd_read_partition(part);
113 if (read == nullptr) {
114 LOGE("failed to open \"%s\": %s\n", v->blk_device, strerror(errno));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800115 return -1;
116 }
117
118 ssize_t size = write_size * MISC_PAGES;
119 char data[size];
120 ssize_t r = mtd_read_data(read, data, size);
Tao Baobd82b272016-01-21 13:49:03 -0800121 if (r != size) LOGE("failed to read \"%s\": %s\n", v->blk_device, strerror(errno));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800122 mtd_read_close(read);
123 if (r != size) return -1;
124
125 memcpy(&data[write_size * MISC_COMMAND_PAGE], in, sizeof(*in));
126
Tao Baobd82b272016-01-21 13:49:03 -0800127 MtdWriteContext* write = mtd_write_partition(part);
128 if (write == nullptr) {
129 LOGE("failed to open \"%s\": %s\n", v->blk_device, strerror(errno));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800130 return -1;
131 }
132 if (mtd_write_data(write, data, size) != size) {
Tao Baobd82b272016-01-21 13:49:03 -0800133 LOGE("failed to write \"%s\": %s\n", v->blk_device, strerror(errno));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800134 mtd_write_close(write);
135 return -1;
136 }
137 if (mtd_write_close(write)) {
Tao Baobd82b272016-01-21 13:49:03 -0800138 LOGE("failed to finish \"%s\": %s\n", v->blk_device, strerror(errno));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800139 return -1;
140 }
141
142 LOGI("Set boot command \"%s\"\n", in->command[0] != 255 ? in->command : "");
143 return 0;
144}
Doug Zongker04611da2010-08-12 15:35:29 -0700145
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700146
147// ------------------------------------
148// for misc partitions on block devices
149// ------------------------------------
150
Doug Zongkercfd256a2011-04-22 09:26:44 -0700151static void wait_for_device(const char* fn) {
152 int tries = 0;
153 int ret;
Doug Zongkercfd256a2011-04-22 09:26:44 -0700154 do {
155 ++tries;
Tao Baobd82b272016-01-21 13:49:03 -0800156 struct stat buf;
Doug Zongkercfd256a2011-04-22 09:26:44 -0700157 ret = stat(fn, &buf);
Tao Baobd82b272016-01-21 13:49:03 -0800158 if (ret == -1) {
159 printf("failed to stat \"%s\" try %d: %s\n", fn, tries, strerror(errno));
Doug Zongkercfd256a2011-04-22 09:26:44 -0700160 sleep(1);
161 }
162 } while (ret && tries < 10);
Tao Baobd82b272016-01-21 13:49:03 -0800163
Doug Zongkercfd256a2011-04-22 09:26:44 -0700164 if (ret) {
Tao Baobd82b272016-01-21 13:49:03 -0800165 printf("failed to stat \"%s\"\n", fn);
Doug Zongkercfd256a2011-04-22 09:26:44 -0700166 }
167}
168
Tao Baobd82b272016-01-21 13:49:03 -0800169static int get_bootloader_message_block(bootloader_message* out,
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700170 const Volume* v) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800171 wait_for_device(v->blk_device);
172 FILE* f = fopen(v->blk_device, "rb");
Tao Baobd82b272016-01-21 13:49:03 -0800173 if (f == nullptr) {
174 LOGE("failed to open \"%s\": %s\n", v->blk_device, strerror(errno));
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700175 return -1;
176 }
Tao Baobd82b272016-01-21 13:49:03 -0800177 bootloader_message temp;
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700178 int count = fread(&temp, sizeof(temp), 1, f);
179 if (count != 1) {
Tao Baobd82b272016-01-21 13:49:03 -0800180 LOGE("failed to read \"%s\": %s\n", v->blk_device, strerror(errno));
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700181 return -1;
182 }
183 if (fclose(f) != 0) {
Tao Baobd82b272016-01-21 13:49:03 -0800184 LOGE("failed to close \"%s\": %s\n", v->blk_device, strerror(errno));
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700185 return -1;
186 }
187 memcpy(out, &temp, sizeof(temp));
188 return 0;
189}
190
Tao Baobd82b272016-01-21 13:49:03 -0800191static int set_bootloader_message_block(const bootloader_message* in,
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700192 const Volume* v) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800193 wait_for_device(v->blk_device);
Elliott Hughesbcabd092016-03-22 20:19:22 -0700194 android::base::unique_fd fd(open(v->blk_device, O_WRONLY | O_SYNC));
195 if (fd == -1) {
Tao Baobd82b272016-01-21 13:49:03 -0800196 LOGE("failed to open \"%s\": %s\n", v->blk_device, strerror(errno));
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700197 return -1;
198 }
Tao Baobd82b272016-01-21 13:49:03 -0800199
200 size_t written = 0;
201 const uint8_t* start = reinterpret_cast<const uint8_t*>(in);
202 size_t total = sizeof(*in);
203 while (written < total) {
Elliott Hughesbcabd092016-03-22 20:19:22 -0700204 ssize_t wrote = TEMP_FAILURE_RETRY(write(fd, start + written, total - written));
Tao Baobd82b272016-01-21 13:49:03 -0800205 if (wrote == -1) {
206 LOGE("failed to write %" PRId64 " bytes: %s\n",
207 static_cast<off64_t>(written), strerror(errno));
208 return -1;
209 }
210 written += wrote;
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700211 }
Tao Baobd82b272016-01-21 13:49:03 -0800212
Elliott Hughesbcabd092016-03-22 20:19:22 -0700213 if (fsync(fd) == -1) {
Tao Baobd82b272016-01-21 13:49:03 -0800214 LOGE("failed to fsync \"%s\": %s\n", v->blk_device, strerror(errno));
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700215 return -1;
216 }
217 return 0;
218}