xref: /OK3568_Linux_fs/external/recovery/bootloader.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
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 "bootloader.h"
18 #include "common.h"
19 #include "mtdutils/mtdutils.h"
20 #include "roots.h"
21 #include "rktools.h"
22 
23 #include <errno.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <sys/stat.h>
27 #include <unistd.h>
28 
29 
30 static int get_bootloader_message_mtd(struct bootloader_message *out);
31 static int set_bootloader_message_mtd(const struct bootloader_message *in);
32 static int get_bootloader_message_block(struct bootloader_message *out);
33 static int set_bootloader_message_block(const struct bootloader_message *in);
34 
get_bootloader_message(struct bootloader_message * out)35 int get_bootloader_message(struct bootloader_message *out)
36 {
37     if (isMtdDevice())
38         return get_bootloader_message_mtd(out);
39 
40     return get_bootloader_message_block(out);
41 }
42 
set_bootloader_message(const struct bootloader_message * in)43 int set_bootloader_message(const struct bootloader_message *in)
44 {
45     if (isMtdDevice())
46         return set_bootloader_message_mtd(in);
47 
48     return set_bootloader_message_block(in);
49 }
50 
51 // ------------------------------
52 // for misc partitions on MTD
53 // ------------------------------
54 
55 // The Bootloader message is at 16K(0x4000) offset, we gonna to read/write 20KB
56 
57 #define MISC_SIZE ((16 + 4) << 10)
58 #define CMD_OFFSET (16 << 10)
59 
60 #define MISC_NAME "misc"
get_bootloader_message_mtd(struct bootloader_message * out)61 static int get_bootloader_message_mtd(struct bootloader_message *out)
62 {
63     size_t write_size;
64     mtd_scan_partitions();
65     const MtdPartition *part = mtd_find_partition_by_name(MISC_NAME);
66     if (part == NULL || mtd_partition_info(part, NULL, NULL, &write_size)) {
67         LOGE("Can't find %s\n", MISC_NAME);
68         return -1;
69     }
70 
71     MtdReadContext *read = mtd_read_partition(part);
72     if (read == NULL) {
73         LOGE("Can't open %s\n(%s)\n", MISC_NAME, strerror(errno));
74         return -1;
75     }
76 
77     char data[MISC_SIZE];
78     //to be align with write_size
79     const ssize_t size = (MISC_SIZE / write_size) * write_size;
80     ssize_t r = mtd_read_data(read, data, size);
81     if (r != size) LOGE("Can't read %s\n(%s)\n", MISC_NAME, strerror(errno));
82     mtd_read_close(read);
83     if (r != size) return -1;
84 
85     memcpy(out, &data[CMD_OFFSET], sizeof(*out));
86     LOGI("out->command = %s.\n", out->command);
87     LOGI("out->status = %s.\n", out->status);
88     LOGI("out->recovery = %s.\n", out->recovery);
89     LOGI("out->systemFlag = %s.\n", out->systemFlag);
90 
91     return 0;
92 }
set_bootloader_message_mtd(const struct bootloader_message * in)93 static int set_bootloader_message_mtd(const struct bootloader_message *in)
94 {
95     size_t write_size;
96     mtd_scan_partitions();
97     const MtdPartition *part = mtd_find_partition_by_name(MISC_NAME);
98     if (part == NULL || mtd_partition_info(part, NULL, NULL, &write_size)) {
99         LOGE("Can't find %s\n", MISC_NAME);
100         return -1;
101     }
102 
103     MtdReadContext *read = mtd_read_partition(part);
104     if (read == NULL) {
105         LOGE("Can't open %s\n(%s)\n", MISC_NAME, strerror(errno));
106         return -1;
107     }
108 
109     char data[MISC_SIZE];
110     //to be align with write_size
111     const ssize_t size = (MISC_SIZE / write_size) * write_size;
112     ssize_t r = mtd_read_data(read, data, size);
113     if (r != size) LOGE("Can't read %s\n(%s)\n", MISC_NAME, strerror(errno));
114     mtd_read_close(read);
115     if (r != size) return -1;
116 
117     memcpy(&data[CMD_OFFSET], in, sizeof(*in));
118 
119     MtdWriteContext *write = mtd_write_partition(part);
120     if (write == NULL) {
121         LOGE("Can't open %s\n(%s)\n", MISC_NAME, strerror(errno));
122         return -1;
123     }
124     if (mtd_write_data(write, data, size) != size) {
125         LOGE("Can't write %s\n(%s)\n", MISC_NAME, strerror(errno));
126         mtd_write_close(write);
127         return -1;
128     }
129     if (mtd_write_close(write)) {
130         LOGE("Can't finish %s\n(%s)\n", MISC_NAME, strerror(errno));
131         return -1;
132     }
133 
134     LOGI("Set boot command \"%s\"\n", in->command[0] != 255 ? in->command : "");
135     return 0;
136 }
137 
138 
139 // ------------------------------------
140 // for misc partitions on block devices
141 // ------------------------------------
wait_for_device(const char * fn)142 static void wait_for_device(const char* fn)
143 {
144     int tries = 0;
145     int ret;
146     struct stat buf;
147     do {
148         ++tries;
149         ret = stat(fn, &buf);
150         if (ret) {
151             LOGI("stat %s try %d: %s\n", fn, tries, strerror(errno));
152             sleep(1);
153         }
154     } while (ret && tries < 10);
155     if (ret) {
156         LOGI("failed to stat %s\n", fn);
157     }
158 }
159 
get_bootloader_message_block(struct bootloader_message * out)160 static int get_bootloader_message_block(struct bootloader_message *out)
161 {
162     wait_for_device(MISC_PARTITION_NAME_BLOCK);
163     FILE* f = fopen(MISC_PARTITION_NAME_BLOCK, "rb");
164     if (f == NULL) {
165         LOGE("Can't open %s\n(%s)\n", MISC_PARTITION_NAME_BLOCK, strerror(errno));
166         return -1;
167     }
168     struct bootloader_message temp;
169     fseek(f, BOOTLOADER_MESSAGE_OFFSET_IN_MISC, SEEK_SET);
170 
171     int count = fread(&temp, sizeof(temp), 1, f);
172     if (count != 1) {
173         LOGE("Failed reading %s\n(%s)\n", MISC_PARTITION_NAME_BLOCK, strerror(errno));
174         return -1;
175     }
176     if (fclose(f) != 0) {
177         LOGE("Failed closing %s\n(%s)\n", MISC_PARTITION_NAME_BLOCK, strerror(errno));
178         return -1;
179     }
180     memcpy(out, &temp, sizeof(temp));
181     return 0;
182 }
183 
set_bootloader_message_block(const struct bootloader_message * in)184 static int set_bootloader_message_block(const struct bootloader_message *in)
185 {
186     FILE* f = fopen(MISC_PARTITION_NAME_BLOCK, "wb");
187     if (f == NULL) {
188         LOGE("Can't open %s\n(%s)\n", MISC_PARTITION_NAME_BLOCK, strerror(errno));
189         return -1;
190     }
191     fseek(f, BOOTLOADER_MESSAGE_OFFSET_IN_MISC, SEEK_SET);
192     int count = fwrite(in, sizeof(*in), 1, f);
193     if (count != 1) {
194         LOGE("Failed writing %s\n(%s)\n", MISC_PARTITION_NAME_BLOCK, strerror(errno));
195         return -1;
196     }
197     if (fclose(f) != 0) {
198         LOGE("Failed closing %s\n(%s)\n", MISC_PARTITION_NAME_BLOCK, strerror(errno));
199         return -1;
200     }
201     return 0;
202 }
203