blob: 826ceca097cf64cc31bffc07163c07b68136f93d [file] [log] [blame]
Dees_Troy7c2dec82012-09-26 09:49:14 -04001/*
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"
19extern "C" {
20#include "mtdutils/mtdutils.h"
21}
22
23#include <errno.h>
24#include <stdio.h>
25#include <string.h>
26#include <sys/stat.h>
27#include <unistd.h>
28#include "partitions.hpp"
29
30static int get_bootloader_message_mtd(struct bootloader_message *out, const TWPartition* Partition);
31static int set_bootloader_message_mtd(const struct bootloader_message *in, const TWPartition* Partition);
32static int get_bootloader_message_block(struct bootloader_message *out, const TWPartition* Partition);
33static int set_bootloader_message_block(const struct bootloader_message *in, const TWPartition* Partition);
34
35int get_bootloader_message(struct bootloader_message *out) {
36 TWPartition* Part = PartitionManager.Find_Partition_By_Path("/misc");
37 if (Part == NULL) {
38 //LOGE("Cannot load volume /misc!\n");
39 return -1;
40 }
41 if (Part->Current_File_System == "mtd") {
42 return get_bootloader_message_mtd(out, Part);
43 } else if (Part->Current_File_System == "emmc") {
44 return get_bootloader_message_block(out, Part);
45 }
46 LOGE("unknown misc partition fs_type \"%s\"\n", Part->Current_File_System.c_str());
47 return -1;
48}
49
50int set_bootloader_message(const struct bootloader_message *in) {
51 TWPartition* Part = PartitionManager.Find_Partition_By_Path("/misc");
52 if (Part == NULL) {
53 //LOGE("Cannot load volume /misc!\n");
54 return -1;
55 }
56 if (Part->Current_File_System == "mtd") {
57 return set_bootloader_message_mtd(in, Part);
58 } else if (Part->Current_File_System == "emmc") {
59 return set_bootloader_message_block(in, Part);
60 }
61 LOGE("unknown misc partition fs_type \"%s\"\n", Part->Current_File_System.c_str());
62 return -1;
63}
64
65// ------------------------------
66// for misc partitions on MTD
67// ------------------------------
68
69static const int MISC_PAGES = 3; // number of pages to save
70static const int MISC_COMMAND_PAGE = 1; // bootloader command is this page
71
72static int get_bootloader_message_mtd(struct bootloader_message *out,
73 const TWPartition* Partition) {
74 size_t write_size;
75 mtd_scan_partitions();
Dees_Troy094207a2012-09-26 12:00:39 -040076 const MtdPartition *part = mtd_find_partition_by_name(Partition->MTD_Name.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -040077 if (part == NULL || mtd_partition_info(part, NULL, NULL, &write_size)) {
Dees_Troy094207a2012-09-26 12:00:39 -040078 LOGE("Can't find %s\n", Partition->MTD_Name.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -040079 return -1;
80 }
81
82 MtdReadContext *read = mtd_read_partition(part);
83 if (read == NULL) {
Dees_Troy094207a2012-09-26 12:00:39 -040084 LOGE("Can't open %s\n(%s)\n", Partition->MTD_Name.c_str(), strerror(errno));
Dees_Troy7c2dec82012-09-26 09:49:14 -040085 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);
Dees_Troy094207a2012-09-26 12:00:39 -040091 if (r != size) LOGE("Can't read %s\n(%s)\n", Partition->MTD_Name.c_str(), strerror(errno));
Dees_Troy7c2dec82012-09-26 09:49:14 -040092 mtd_read_close(read);
93 if (r != size) return -1;
94
95 memcpy(out, &data[write_size * MISC_COMMAND_PAGE], sizeof(*out));
96 return 0;
97}
98static int set_bootloader_message_mtd(const struct bootloader_message *in,
99 const TWPartition* Partition) {
100 size_t write_size;
101 mtd_scan_partitions();
Dees_Troy094207a2012-09-26 12:00:39 -0400102 const MtdPartition *part = mtd_find_partition_by_name(Partition->MTD_Name.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -0400103 if (part == NULL || mtd_partition_info(part, NULL, NULL, &write_size)) {
Dees_Troy094207a2012-09-26 12:00:39 -0400104 LOGE("Can't find %s\n", Partition->MTD_Name.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -0400105 return -1;
106 }
107
108 MtdReadContext *read = mtd_read_partition(part);
109 if (read == NULL) {
Dees_Troy094207a2012-09-26 12:00:39 -0400110 LOGE("Can't open %s\n(%s)\n", Partition->MTD_Name.c_str(), strerror(errno));
Dees_Troy7c2dec82012-09-26 09:49:14 -0400111 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);
Dees_Troy094207a2012-09-26 12:00:39 -0400117 if (r != size) LOGE("Can't read %s\n(%s)\n", Partition->MTD_Name.c_str(), strerror(errno));
Dees_Troy7c2dec82012-09-26 09:49:14 -0400118 mtd_read_close(read);
119 if (r != size) return -1;
120
121 memcpy(&data[write_size * MISC_COMMAND_PAGE], in, sizeof(*in));
122
123 MtdWriteContext *write = mtd_write_partition(part);
124 if (write == NULL) {
Dees_Troy094207a2012-09-26 12:00:39 -0400125 LOGE("Can't open %s\n(%s)\n", Partition->MTD_Name.c_str(), strerror(errno));
Dees_Troy7c2dec82012-09-26 09:49:14 -0400126 return -1;
127 }
128 if (mtd_write_data(write, data, size) != size) {
Dees_Troy094207a2012-09-26 12:00:39 -0400129 LOGE("Can't write %s\n(%s)\n", Partition->MTD_Name.c_str(), strerror(errno));
Dees_Troy7c2dec82012-09-26 09:49:14 -0400130 mtd_write_close(write);
131 return -1;
132 }
133 if (mtd_write_close(write)) {
Dees_Troy094207a2012-09-26 12:00:39 -0400134 LOGE("Can't finish %s\n(%s)\n", Partition->MTD_Name.c_str(), strerror(errno));
Dees_Troy7c2dec82012-09-26 09:49:14 -0400135 return -1;
136 }
137
138 LOGI("Set boot command \"%s\"\n", in->command[0] != 255 ? in->command : "");
139 return 0;
140}
141
142
143// ------------------------------------
144// for misc partitions on block devices
145// ------------------------------------
146
147static 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
164static int get_bootloader_message_block(struct bootloader_message *out,
165 const TWPartition* Partition) {
166 wait_for_device(Partition->Actual_Block_Device.c_str());
167 FILE* f = fopen(Partition->Actual_Block_Device.c_str(), "rb");
168 if (f == NULL) {
169 LOGE("Can't open %s\n(%s)\n", Partition->Actual_Block_Device.c_str(), 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", Partition->Actual_Block_Device.c_str(), strerror(errno));
176 return -1;
177 }
178 if (fclose(f) != 0) {
179 LOGE("Failed closing %s\n(%s)\n", Partition->Actual_Block_Device.c_str(), 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 TWPartition* Partition) {
188 wait_for_device(Partition->Actual_Block_Device.c_str());
189 FILE* f = fopen(Partition->Actual_Block_Device.c_str(), "wb");
190 if (f == NULL) {
191 LOGE("Can't open %s\n(%s)\n", Partition->Actual_Block_Device.c_str(), 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", Partition->Actual_Block_Device.c_str(), strerror(errno));
197 return -1;
198 }
199 if (fclose(f) != 0) {
200 LOGE("Failed closing %s\n(%s)\n", Partition->Actual_Block_Device.c_str(), strerror(errno));
201 return -1;
202 }
203 return 0;
204}