1 /*
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 <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 
26 #include <fs_mgr.h>
27 
28 #include "bootloader.h"
29 #include "common.h"
30 #include "mtdutils/mtdutils.h"
31 #include "roots.h"
32 #include "unique_fd.h"
33 
34 static int get_bootloader_message_mtd(bootloader_message* out, const Volume* v);
35 static int set_bootloader_message_mtd(const bootloader_message* in, const Volume* v);
36 static int get_bootloader_message_block(bootloader_message* out, const Volume* v);
37 static int set_bootloader_message_block(const bootloader_message* in, const Volume* v);
38 
get_bootloader_message(bootloader_message * out)39 int get_bootloader_message(bootloader_message* out) {
40     Volume* v = volume_for_path("/misc");
41     if (v == nullptr) {
42         LOGE("Cannot load volume /misc!\n");
43         return -1;
44     }
45     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);
49     }
50     LOGE("unknown misc partition fs_type \"%s\"\n", v->fs_type);
51     return -1;
52 }
53 
set_bootloader_message(const bootloader_message * in)54 int set_bootloader_message(const bootloader_message* in) {
55     Volume* v = volume_for_path("/misc");
56     if (v == nullptr) {
57         LOGE("Cannot load volume /misc!\n");
58         return -1;
59     }
60     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);
64     }
65     LOGE("unknown misc partition fs_type \"%s\"\n", v->fs_type);
66     return -1;
67 }
68 
69 // ------------------------------
70 // for misc partitions on MTD
71 // ------------------------------
72 
73 static const int MISC_PAGES = 3;         // number of pages to save
74 static const int MISC_COMMAND_PAGE = 1;  // bootloader command is this page
75 
get_bootloader_message_mtd(bootloader_message * out,const Volume * v)76 static int get_bootloader_message_mtd(bootloader_message* out,
77                                       const Volume* v) {
78     size_t write_size;
79     mtd_scan_partitions();
80     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);
83         return -1;
84     }
85 
86     MtdReadContext* read = mtd_read_partition(part);
87     if (read == nullptr) {
88         LOGE("failed to open \"%s\": %s\n", v->blk_device, strerror(errno));
89         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);
95     if (r != size) LOGE("failed to read \"%s\": %s\n", v->blk_device, strerror(errno));
96     mtd_read_close(read);
97     if (r != size) return -1;
98 
99     memcpy(out, &data[write_size * MISC_COMMAND_PAGE], sizeof(*out));
100     return 0;
101 }
set_bootloader_message_mtd(const bootloader_message * in,const Volume * v)102 static int set_bootloader_message_mtd(const bootloader_message* in,
103                                       const Volume* v) {
104     size_t write_size;
105     mtd_scan_partitions();
106     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);
109         return -1;
110     }
111 
112     MtdReadContext* read = mtd_read_partition(part);
113     if (read == nullptr) {
114         LOGE("failed to open \"%s\": %s\n", v->blk_device, strerror(errno));
115         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);
121     if (r != size) LOGE("failed to read \"%s\": %s\n", v->blk_device, strerror(errno));
122     mtd_read_close(read);
123     if (r != size) return -1;
124 
125     memcpy(&data[write_size * MISC_COMMAND_PAGE], in, sizeof(*in));
126 
127     MtdWriteContext* write = mtd_write_partition(part);
128     if (write == nullptr) {
129         LOGE("failed to open \"%s\": %s\n", v->blk_device, strerror(errno));
130         return -1;
131     }
132     if (mtd_write_data(write, data, size) != size) {
133         LOGE("failed to write \"%s\": %s\n", v->blk_device, strerror(errno));
134         mtd_write_close(write);
135         return -1;
136     }
137     if (mtd_write_close(write)) {
138         LOGE("failed to finish \"%s\": %s\n", v->blk_device, strerror(errno));
139         return -1;
140     }
141 
142     LOGI("Set boot command \"%s\"\n", in->command[0] != 255 ? in->command : "");
143     return 0;
144 }
145 
146 
147 // ------------------------------------
148 // for misc partitions on block devices
149 // ------------------------------------
150 
wait_for_device(const char * fn)151 static void wait_for_device(const char* fn) {
152     int tries = 0;
153     int ret;
154     do {
155         ++tries;
156         struct stat buf;
157         ret = stat(fn, &buf);
158         if (ret == -1) {
159             printf("failed to stat \"%s\" try %d: %s\n", fn, tries, strerror(errno));
160             sleep(1);
161         }
162     } while (ret && tries < 10);
163 
164     if (ret) {
165         printf("failed to stat \"%s\"\n", fn);
166     }
167 }
168 
get_bootloader_message_block(bootloader_message * out,const Volume * v)169 static int get_bootloader_message_block(bootloader_message* out,
170                                         const Volume* v) {
171     wait_for_device(v->blk_device);
172     FILE* f = fopen(v->blk_device, "rb");
173     if (f == nullptr) {
174         LOGE("failed to open \"%s\": %s\n", v->blk_device, strerror(errno));
175         return -1;
176     }
177     bootloader_message temp;
178     int count = fread(&temp, sizeof(temp), 1, f);
179     if (count != 1) {
180         LOGE("failed to read \"%s\": %s\n", v->blk_device, strerror(errno));
181         return -1;
182     }
183     if (fclose(f) != 0) {
184         LOGE("failed to close \"%s\": %s\n", v->blk_device, strerror(errno));
185         return -1;
186     }
187     memcpy(out, &temp, sizeof(temp));
188     return 0;
189 }
190 
set_bootloader_message_block(const bootloader_message * in,const Volume * v)191 static int set_bootloader_message_block(const bootloader_message* in,
192                                         const Volume* v) {
193     wait_for_device(v->blk_device);
194     unique_fd fd(open(v->blk_device, O_WRONLY | O_SYNC));
195     if (fd.get() == -1) {
196         LOGE("failed to open \"%s\": %s\n", v->blk_device, strerror(errno));
197         return -1;
198     }
199 
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) {
204         ssize_t wrote = TEMP_FAILURE_RETRY(write(fd.get(), start + written, total - written));
205         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;
211     }
212 
213     if (fsync(fd.get()) == -1) {
214         LOGE("failed to fsync \"%s\": %s\n", v->blk_device, strerror(errno));
215         return -1;
216     }
217     return 0;
218 }
219