blob: 70965660212e5b0e80bce114692dfff7241e906f [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 Zongkercc8cd3f2010-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);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080032
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070033int get_bootloader_message(struct bootloader_message *out) {
34 Volume* v = volume_for_path("/misc");
35 if (strcmp(v->fs_type, "mtd") == 0) {
36 return get_bootloader_message_mtd(out, v);
37 } else if (strcmp(v->fs_type, "emmc") == 0) {
38 return get_bootloader_message_block(out, v);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080039 }
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070040 LOGE("unknown misc partition fs_type \"%s\"\n", v->fs_type);
41 return -1;
Doug Zongker04611da2010-08-12 15:35:29 -070042}
43
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070044int set_bootloader_message(const struct bootloader_message *in) {
45 Volume* v = volume_for_path("/misc");
46 if (strcmp(v->fs_type, "mtd") == 0) {
47 return set_bootloader_message_mtd(in, v);
48 } else if (strcmp(v->fs_type, "emmc") == 0) {
49 return set_bootloader_message_block(in, v);
Doug Zongker04611da2010-08-12 15:35:29 -070050 }
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070051 LOGE("unknown misc partition fs_type \"%s\"\n", v->fs_type);
52 return -1;
Doug Zongker04611da2010-08-12 15:35:29 -070053}
54
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070055// ------------------------------
56// for misc partitions on MTD
57// ------------------------------
Doug Zongker04611da2010-08-12 15:35:29 -070058
59static const int MISC_PAGES = 3; // number of pages to save
60static const int MISC_COMMAND_PAGE = 1; // bootloader command is this page
61
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070062static int get_bootloader_message_mtd(struct bootloader_message *out,
63 const Volume* v) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080064 size_t write_size;
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070065 mtd_scan_partitions();
66 const MtdPartition *part = mtd_find_partition_by_name(v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080067 if (part == NULL || mtd_partition_info(part, NULL, NULL, &write_size)) {
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070068 LOGE("Can't find %s\n", v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080069 return -1;
70 }
71
72 MtdReadContext *read = mtd_read_partition(part);
73 if (read == NULL) {
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070074 LOGE("Can't open %s\n(%s)\n", v->device, strerror(errno));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080075 return -1;
76 }
77
78 const ssize_t size = write_size * MISC_PAGES;
79 char data[size];
80 ssize_t r = mtd_read_data(read, data, size);
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070081 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 -080082 mtd_read_close(read);
83 if (r != size) return -1;
84
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080085 memcpy(out, &data[write_size * MISC_COMMAND_PAGE], sizeof(*out));
86 return 0;
87}
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070088static int set_bootloader_message_mtd(const struct bootloader_message *in,
89 const Volume* v) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080090 size_t write_size;
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070091 mtd_scan_partitions();
92 const MtdPartition *part = mtd_find_partition_by_name(v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080093 if (part == NULL || mtd_partition_info(part, NULL, NULL, &write_size)) {
Doug Zongkercc8cd3f2010-09-20 12:16:13 -070094 LOGE("Can't find %s\n", v->device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080095 return -1;
96 }
97
98 MtdReadContext *read = mtd_read_partition(part);
99 if (read == NULL) {
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700100 LOGE("Can't open %s\n(%s)\n", v->device, strerror(errno));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800101 return -1;
102 }
103
104 ssize_t size = write_size * MISC_PAGES;
105 char data[size];
106 ssize_t r = mtd_read_data(read, data, size);
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700107 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 -0800108 mtd_read_close(read);
109 if (r != size) return -1;
110
111 memcpy(&data[write_size * MISC_COMMAND_PAGE], in, sizeof(*in));
112
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800113 MtdWriteContext *write = mtd_write_partition(part);
114 if (write == NULL) {
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700115 LOGE("Can't open %s\n(%s)\n", v->device, strerror(errno));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800116 return -1;
117 }
118 if (mtd_write_data(write, data, size) != size) {
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700119 LOGE("Can't write %s\n(%s)\n", v->device, strerror(errno));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800120 mtd_write_close(write);
121 return -1;
122 }
123 if (mtd_write_close(write)) {
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700124 LOGE("Can't finish %s\n(%s)\n", v->device, strerror(errno));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800125 return -1;
126 }
127
128 LOGI("Set boot command \"%s\"\n", in->command[0] != 255 ? in->command : "");
129 return 0;
130}
Doug Zongker04611da2010-08-12 15:35:29 -0700131
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700132
133// ------------------------------------
134// for misc partitions on block devices
135// ------------------------------------
136
Doug Zongkercfd256a2011-04-22 09:26:44 -0700137static void wait_for_device(const char* fn) {
138 int tries = 0;
139 int ret;
140 struct stat buf;
141 do {
142 ++tries;
143 ret = stat(fn, &buf);
144 if (ret) {
145 printf("stat %s try %d: %s\n", fn, tries, strerror(errno));
146 sleep(1);
147 }
148 } while (ret && tries < 10);
149 if (ret) {
150 printf("failed to stat %s\n", fn);
151 }
152}
153
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700154static int get_bootloader_message_block(struct bootloader_message *out,
155 const Volume* v) {
Doug Zongkercfd256a2011-04-22 09:26:44 -0700156 wait_for_device(v->device);
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700157 FILE* f = fopen(v->device, "rb");
158 if (f == NULL) {
159 LOGE("Can't open %s\n(%s)\n", v->device, strerror(errno));
160 return -1;
161 }
162 struct bootloader_message temp;
163 int count = fread(&temp, sizeof(temp), 1, f);
164 if (count != 1) {
165 LOGE("Failed reading %s\n(%s)\n", v->device, strerror(errno));
166 return -1;
167 }
168 if (fclose(f) != 0) {
169 LOGE("Failed closing %s\n(%s)\n", v->device, strerror(errno));
170 return -1;
171 }
172 memcpy(out, &temp, sizeof(temp));
173 return 0;
174}
175
176static int set_bootloader_message_block(const struct bootloader_message *in,
177 const Volume* v) {
Doug Zongkercfd256a2011-04-22 09:26:44 -0700178 wait_for_device(v->device);
Doug Zongkercc8cd3f2010-09-20 12:16:13 -0700179 FILE* f = fopen(v->device, "wb");
180 if (f == NULL) {
181 LOGE("Can't open %s\n(%s)\n", v->device, strerror(errno));
182 return -1;
183 }
184 int count = fwrite(in, sizeof(*in), 1, f);
185 if (count != 1) {
186 LOGE("Failed writing %s\n(%s)\n", v->device, strerror(errno));
187 return -1;
188 }
189 if (fclose(f) != 0) {
190 LOGE("Failed closing %s\n(%s)\n", v->device, strerror(errno));
191 return -1;
192 }
193 return 0;
194}