blob: 6f4f83abfc9a41d74c6cddbfce6c03975a3d6a3f [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"
Dees_Troy51a0e822012-09-05 15:24:24 -040019extern "C" {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080020#include "mtdutils/mtdutils.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040021}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080022#include "roots.h"
23
24#include <errno.h>
25#include <stdio.h>
26#include <string.h>
Doug Zongkercfd256a2011-04-22 09:26:44 -070027#include <sys/stat.h>
28#include <unistd.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080029
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070030static int get_bootloader_message_mtd(struct bootloader_message *out, const Volume* v);
31static int set_bootloader_message_mtd(const struct bootloader_message *in, const Volume* v);
32static int get_bootloader_message_block(struct bootloader_message *out, const Volume* v);
33static int set_bootloader_message_block(const struct bootloader_message *in, const Volume* v);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080034
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070035int get_bootloader_message(struct bootloader_message *out) {
36 Volume* v = volume_for_path("/misc");
Adam Blissb2ceb692011-07-13 15:13:54 -070037 if (v == NULL) {
38 LOGE("Cannot load volume /misc!\n");
39 return -1;
40 }
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070041 if (strcmp(v->fs_type, "mtd") == 0) {
42 return get_bootloader_message_mtd(out, v);
43 } else if (strcmp(v->fs_type, "emmc") == 0) {
44 return get_bootloader_message_block(out, v);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080045 }
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070046 LOGE("unknown misc partition fs_type \"%s\"\n", v->fs_type);
47 return -1;
Doug Zongker04611da2010-08-12 15:35:29 -070048}
49
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070050int set_bootloader_message(const struct bootloader_message *in) {
51 Volume* v = volume_for_path("/misc");
Adam Blissb2ceb692011-07-13 15:13:54 -070052 if (v == NULL) {
53 LOGE("Cannot load volume /misc!\n");
54 return -1;
55 }
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070056 if (strcmp(v->fs_type, "mtd") == 0) {
57 return set_bootloader_message_mtd(in, v);
58 } else if (strcmp(v->fs_type, "emmc") == 0) {
59 return set_bootloader_message_block(in, v);
Doug Zongker04611da2010-08-12 15:35:29 -070060 }
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070061 LOGE("unknown misc partition fs_type \"%s\"\n", v->fs_type);
62 return -1;
Doug Zongker04611da2010-08-12 15:35:29 -070063}
64
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070065// ------------------------------
66// for misc partitions on MTD
67// ------------------------------
Doug Zongker04611da2010-08-12 15:35:29 -070068
69static const int MISC_PAGES = 3; // number of pages to save
70static const int MISC_COMMAND_PAGE = 1; // bootloader command is this page
71
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070072static int get_bootloader_message_mtd(struct bootloader_message *out,
73 const Volume* v) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080074 size_t write_size;
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070075 mtd_scan_partitions();
76 const MtdPartition *part = mtd_find_partition_by_name(v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080077 if (part == NULL || mtd_partition_info(part, NULL, NULL, &write_size)) {
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070078 LOGE("Can't find %s\n", v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080079 return -1;
80 }
81
82 MtdReadContext *read = mtd_read_partition(part);
83 if (read == NULL) {
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070084 LOGE("Can't open %s\n(%s)\n", v->device, strerror(errno));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080085 return -1;
86 }
87
88 const ssize_t size = write_size * MISC_PAGES;
89 char data[size];
90 ssize_t r = mtd_read_data(read, data, size);
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070091 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 -080092 mtd_read_close(read);
93 if (r != size) return -1;
94
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080095 memcpy(out, &data[write_size * MISC_COMMAND_PAGE], sizeof(*out));
96 return 0;
97}
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070098static int set_bootloader_message_mtd(const struct bootloader_message *in,
99 const Volume* v) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800100 size_t write_size;
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700101 mtd_scan_partitions();
102 const MtdPartition *part = mtd_find_partition_by_name(v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800103 if (part == NULL || mtd_partition_info(part, NULL, NULL, &write_size)) {
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700104 LOGE("Can't find %s\n", v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800105 return -1;
106 }
107
108 MtdReadContext *read = mtd_read_partition(part);
109 if (read == NULL) {
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700110 LOGE("Can't open %s\n(%s)\n", v->device, strerror(errno));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800111 return -1;
112 }
113
114 ssize_t size = write_size * MISC_PAGES;
115 char data[size];
116 ssize_t r = mtd_read_data(read, data, size);
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700117 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 -0800118 mtd_read_close(read);
119 if (r != size) return -1;
120
121 memcpy(&data[write_size * MISC_COMMAND_PAGE], in, sizeof(*in));
122
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800123 MtdWriteContext *write = mtd_write_partition(part);
124 if (write == NULL) {
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700125 LOGE("Can't open %s\n(%s)\n", v->device, strerror(errno));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800126 return -1;
127 }
128 if (mtd_write_data(write, data, size) != size) {
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700129 LOGE("Can't write %s\n(%s)\n", v->device, strerror(errno));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800130 mtd_write_close(write);
131 return -1;
132 }
133 if (mtd_write_close(write)) {
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700134 LOGE("Can't finish %s\n(%s)\n", v->device, strerror(errno));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800135 return -1;
136 }
137
138 LOGI("Set boot command \"%s\"\n", in->command[0] != 255 ? in->command : "");
139 return 0;
140}
Doug Zongker04611da2010-08-12 15:35:29 -0700141
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700142
143// ------------------------------------
144// for misc partitions on block devices
145// ------------------------------------
146
Doug Zongkercfd256a2011-04-22 09:26:44 -0700147static void wait_for_device(const char* fn) {
148 int tries = 0;
149 int ret;
150 struct stat buf;
151 do {
152 ++tries;
153 ret = stat(fn, &buf);
154 if (ret) {
155 printf("stat %s try %d: %s\n", fn, tries, strerror(errno));
156 sleep(1);
157 }
158 } while (ret && tries < 10);
159 if (ret) {
160 printf("failed to stat %s\n", fn);
161 }
162}
163
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700164static int get_bootloader_message_block(struct bootloader_message *out,
165 const Volume* v) {
Doug Zongkercfd256a2011-04-22 09:26:44 -0700166 wait_for_device(v->device);
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700167 FILE* f = fopen(v->device, "rb");
168 if (f == NULL) {
169 LOGE("Can't open %s\n(%s)\n", v->device, strerror(errno));
170 return -1;
171 }
172 struct bootloader_message temp;
173 int count = fread(&temp, sizeof(temp), 1, f);
174 if (count != 1) {
175 LOGE("Failed reading %s\n(%s)\n", v->device, strerror(errno));
176 return -1;
177 }
178 if (fclose(f) != 0) {
179 LOGE("Failed closing %s\n(%s)\n", v->device, strerror(errno));
180 return -1;
181 }
182 memcpy(out, &temp, sizeof(temp));
183 return 0;
184}
185
186static int set_bootloader_message_block(const struct bootloader_message *in,
187 const Volume* v) {
Doug Zongkercfd256a2011-04-22 09:26:44 -0700188 wait_for_device(v->device);
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700189 FILE* f = fopen(v->device, "wb");
190 if (f == NULL) {
191 LOGE("Can't open %s\n(%s)\n", v->device, strerror(errno));
192 return -1;
193 }
194 int count = fwrite(in, sizeof(*in), 1, f);
195 if (count != 1) {
196 LOGE("Failed writing %s\n(%s)\n", v->device, strerror(errno));
197 return -1;
198 }
199 if (fclose(f) != 0) {
200 LOGE("Failed closing %s\n(%s)\n", v->device, strerror(errno));
201 return -1;
202 }
203 return 0;
204}