blob: d0e49e0cdf9ce35d65f412448fdaba5c9856c0bb [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) {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020036 TWPartition* Part = PartitionManager.Find_Partition_By_Path("/misc");
37 if (Part == NULL) {
38 //LOGE("Cannot load volume /misc!\n");
39 return -1;
40 }
41
42 if (Part->Current_File_System == "mtd")
43 return get_bootloader_message_mtd(out, Part);
44 else if (Part->Current_File_System == "emmc")
45 return get_bootloader_message_block(out, Part);
46
47 LOGE("unknown misc partition fs_type \"%s\"\n", Part->Current_File_System.c_str());
48 return -1;
Dees_Troy7c2dec82012-09-26 09:49:14 -040049}
50
51int set_bootloader_message(const struct bootloader_message *in) {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020052 TWPartition* Part = PartitionManager.Find_Partition_By_Path("/misc");
53 if (Part == NULL) {
54 //LOGE("Cannot load volume /misc!\n");
55 return -1;
56 }
57
58 if (Part->Current_File_System == "mtd")
59 return set_bootloader_message_mtd(in, Part);
60 else if (Part->Current_File_System == "emmc")
61 return set_bootloader_message_block(in, Part);
62
63 LOGE("unknown misc partition fs_type \"%s\"\n", Part->Current_File_System.c_str());
64 return -1;
Dees_Troy7c2dec82012-09-26 09:49:14 -040065}
66
67// ------------------------------
68// for misc partitions on MTD
69// ------------------------------
70
71static const int MISC_PAGES = 3; // number of pages to save
72static const int MISC_COMMAND_PAGE = 1; // bootloader command is this page
73
74static int get_bootloader_message_mtd(struct bootloader_message *out,
75 const TWPartition* Partition) {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020076 size_t write_size;
77 mtd_scan_partitions();
78 const MtdPartition *part = mtd_find_partition_by_name(Partition->MTD_Name.c_str());
79 if (part == NULL || mtd_partition_info(part, NULL, NULL, &write_size)) {
80 LOGE("Can't find %s\n", Partition->MTD_Name.c_str());
81 return -1;
82 }
Dees_Troy7c2dec82012-09-26 09:49:14 -040083
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020084 MtdReadContext *read = mtd_read_partition(part);
85 if (read == NULL) {
86 LOGE("Can't open %s\n(%s)\n", Partition->MTD_Name.c_str(), strerror(errno));
87 return -1;
88 }
Dees_Troy7c2dec82012-09-26 09:49:14 -040089
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020090 const ssize_t size = write_size * MISC_PAGES;
91 char data[size];
92 ssize_t r = mtd_read_data(read, data, size);
93 if (r != size) LOGE("Can't read %s\n(%s)\n", Partition->MTD_Name.c_str(), strerror(errno));
94 mtd_read_close(read);
95 if (r != size) return -1;
Dees_Troy7c2dec82012-09-26 09:49:14 -040096
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020097 memcpy(out, &data[write_size * MISC_COMMAND_PAGE], sizeof(*out));
98 return 0;
Dees_Troy7c2dec82012-09-26 09:49:14 -040099}
100static int set_bootloader_message_mtd(const struct bootloader_message *in,
101 const TWPartition* Partition) {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200102 size_t write_size;
103 mtd_scan_partitions();
104 const MtdPartition *part = mtd_find_partition_by_name(Partition->MTD_Name.c_str());
105 if (part == NULL || mtd_partition_info(part, NULL, NULL, &write_size)) {
106 LOGE("Can't find %s\n", Partition->MTD_Name.c_str());
107 return -1;
108 }
Dees_Troy7c2dec82012-09-26 09:49:14 -0400109
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200110 MtdReadContext *read = mtd_read_partition(part);
111 if (read == NULL) {
112 LOGE("Can't open %s\n(%s)\n", Partition->MTD_Name.c_str(), strerror(errno));
113 return -1;
114 }
Dees_Troy7c2dec82012-09-26 09:49:14 -0400115
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200116 ssize_t size = write_size * MISC_PAGES;
117 char data[size];
118 ssize_t r = mtd_read_data(read, data, size);
119 if (r != size) LOGE("Can't read %s\n(%s)\n", Partition->MTD_Name.c_str(), strerror(errno));
120 mtd_read_close(read);
121 if (r != size) return -1;
Dees_Troy7c2dec82012-09-26 09:49:14 -0400122
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200123 memcpy(&data[write_size * MISC_COMMAND_PAGE], in, sizeof(*in));
Dees_Troy7c2dec82012-09-26 09:49:14 -0400124
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200125 MtdWriteContext *write = mtd_write_partition(part);
126 if (write == NULL) {
127 LOGE("Can't open %s\n(%s)\n", Partition->MTD_Name.c_str(), strerror(errno));
128 return -1;
129 }
130 if (mtd_write_data(write, data, size) != size) {
131 LOGE("Can't write %s\n(%s)\n", Partition->MTD_Name.c_str(), strerror(errno));
132 mtd_write_close(write);
133 return -1;
134 }
135 if (mtd_write_close(write)) {
136 LOGE("Can't finish %s\n(%s)\n", Partition->MTD_Name.c_str(), strerror(errno));
137 return -1;
138 }
Dees_Troy7c2dec82012-09-26 09:49:14 -0400139
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200140 LOGI("Set boot command \"%s\"\n", in->command[0] != 255 ? in->command : "");
141 return 0;
Dees_Troy7c2dec82012-09-26 09:49:14 -0400142}
143
Dees_Troy7c2dec82012-09-26 09:49:14 -0400144// ------------------------------------
145// for misc partitions on block devices
146// ------------------------------------
147
148static void wait_for_device(const char* fn) {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200149 int tries = 0;
150 int ret;
151 struct stat buf;
152
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
162 if (ret)
163 printf("failed to stat %s\n", fn);
Dees_Troy7c2dec82012-09-26 09:49:14 -0400164}
165
166static int get_bootloader_message_block(struct bootloader_message *out,
167 const TWPartition* Partition) {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200168 wait_for_device(Partition->Actual_Block_Device.c_str());
169
170 FILE* f = fopen(Partition->Actual_Block_Device.c_str(), "rb");
171 if (f == NULL) {
172 LOGE("Can't open %s\n(%s)\n", Partition->Actual_Block_Device.c_str(), strerror(errno));
173 return -1;
174 }
175
176 struct bootloader_message temp;
177 int count = fread(&temp, sizeof(temp), 1, f);
178 if (count != 1) {
179 LOGE("Failed reading %s\n(%s)\n", Partition->Actual_Block_Device.c_str(), strerror(errno));
180 return -1;
181 }
182
183 if (fclose(f) != 0) {
184 LOGE("Failed closing %s\n(%s)\n", Partition->Actual_Block_Device.c_str(), strerror(errno));
185 return -1;
186 }
187
188 memcpy(out, &temp, sizeof(temp));
189 return 0;
Dees_Troy7c2dec82012-09-26 09:49:14 -0400190}
191
192static int set_bootloader_message_block(const struct bootloader_message *in,
193 const TWPartition* Partition) {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200194 wait_for_device(Partition->Actual_Block_Device.c_str());
195
196 FILE* f = fopen(Partition->Actual_Block_Device.c_str(), "wb");
197 if (f == NULL) {
198 LOGE("Can't open %s\n(%s)\n", Partition->Actual_Block_Device.c_str(), strerror(errno));
199 return -1;
200 }
201
202 int count = fwrite(in, sizeof(*in), 1, f);
203 if (count != 1) {
204 LOGE("Failed writing %s\n(%s)\n", Partition->Actual_Block_Device.c_str(), strerror(errno));
205 return -1;
206 }
207
208 if (fclose(f) != 0) {
209 LOGE("Failed closing %s\n(%s)\n", Partition->Actual_Block_Device.c_str(), strerror(errno));
210 return -1;
211 }
212 return 0;
Dees_Troy7c2dec82012-09-26 09:49:14 -0400213}