blob: 1e66c3e26b5f7801374dd6d91806b676ddef6284 [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>
25
26static const char *CACHE_NAME = "CACHE:";
27static const char *MISC_NAME = "MISC:";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080028
29#ifdef LOG_VERBOSE
30static void dump_data(const char *data, int len) {
31 int pos;
32 for (pos = 0; pos < len; ) {
33 printf("%05x: %02x", pos, data[pos]);
34 for (++pos; pos < len && (pos % 24) != 0; ++pos) {
35 printf(" %02x", data[pos]);
36 }
37 printf("\n");
38 }
39}
40#endif
41
Doug Zongker04611da2010-08-12 15:35:29 -070042#ifdef USE_EXT4
43// Strictly speaking this doesn't have anything to do with ext4; we
44// really just mean "misc is an emmc partition". We should have a
45// more configurable way have describing partitions, filesystems, etc.
46
47static const char* MISC_PARTITION =
48 "/dev/block/platform/sdhci-tegra.3/by-name/misc";
49
50int get_bootloader_message(struct bootloader_message* out) {
51 FILE* f = fopen(MISC_PARTITION, "rb");
52 if (f == NULL) {
53 LOGE("Can't open %s\n(%s)\n", MISC_PARTITION, strerror(errno));
54 return -1;
55 }
56 struct bootloader_message temp;
57 int count = fread(&temp, sizeof(temp), 1, f);
58 if (count != 1) {
59 LOGE("Failed reading %s\n(%s)\n", MISC_PARTITION, strerror(errno));
60 return -1;
61 }
62 if (fclose(f) != 0) {
63 LOGE("Failed closing %s\n(%s)\n", MISC_PARTITION, strerror(errno));
64 return -1;
65 }
66 memcpy(out, &temp, sizeof(temp));
67 return 0;
68}
69
70int set_bootloader_message(const struct bootloader_message* in) {
71 FILE* f = fopen(MISC_PARTITION, "wb");
72 if (f == NULL) {
73 LOGE("Can't open %s\n(%s)\n", MISC_PARTITION, strerror(errno));
74 return -1;
75 }
76 int count = fwrite(in, sizeof(*in), 1, f);
77 if (count != 1) {
78 LOGE("Failed writing %s\n(%s)\n", MISC_PARTITION, strerror(errno));
79 return -1;
80 }
81 if (fclose(f) != 0) {
82 LOGE("Failed closing %s\n(%s)\n", MISC_PARTITION, strerror(errno));
83 return -1;
84 }
85 return 0;
86}
87
88#else // MTD partitions
89
90static const int MISC_PAGES = 3; // number of pages to save
91static const int MISC_COMMAND_PAGE = 1; // bootloader command is this page
92
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080093int get_bootloader_message(struct bootloader_message *out) {
94 size_t write_size;
95 const MtdPartition *part = get_root_mtd_partition(MISC_NAME);
96 if (part == NULL || mtd_partition_info(part, NULL, NULL, &write_size)) {
97 LOGE("Can't find %s\n", MISC_NAME);
98 return -1;
99 }
100
101 MtdReadContext *read = mtd_read_partition(part);
102 if (read == NULL) {
103 LOGE("Can't open %s\n(%s)\n", MISC_NAME, strerror(errno));
104 return -1;
105 }
106
107 const ssize_t size = write_size * MISC_PAGES;
108 char data[size];
109 ssize_t r = mtd_read_data(read, data, size);
110 if (r != size) LOGE("Can't read %s\n(%s)\n", MISC_NAME, strerror(errno));
111 mtd_read_close(read);
112 if (r != size) return -1;
113
114#ifdef LOG_VERBOSE
115 printf("\n--- get_bootloader_message ---\n");
116 dump_data(data, size);
117 printf("\n");
118#endif
119
120 memcpy(out, &data[write_size * MISC_COMMAND_PAGE], sizeof(*out));
121 return 0;
122}
123
124int set_bootloader_message(const struct bootloader_message *in) {
125 size_t write_size;
126 const MtdPartition *part = get_root_mtd_partition(MISC_NAME);
127 if (part == NULL || mtd_partition_info(part, NULL, NULL, &write_size)) {
128 LOGE("Can't find %s\n", MISC_NAME);
129 return -1;
130 }
131
132 MtdReadContext *read = mtd_read_partition(part);
133 if (read == NULL) {
134 LOGE("Can't open %s\n(%s)\n", MISC_NAME, strerror(errno));
135 return -1;
136 }
137
138 ssize_t size = write_size * MISC_PAGES;
139 char data[size];
140 ssize_t r = mtd_read_data(read, data, size);
141 if (r != size) LOGE("Can't read %s\n(%s)\n", MISC_NAME, strerror(errno));
142 mtd_read_close(read);
143 if (r != size) return -1;
144
145 memcpy(&data[write_size * MISC_COMMAND_PAGE], in, sizeof(*in));
146
147#ifdef LOG_VERBOSE
148 printf("\n--- set_bootloader_message ---\n");
149 dump_data(data, size);
150 printf("\n");
151#endif
152
153 MtdWriteContext *write = mtd_write_partition(part);
154 if (write == NULL) {
155 LOGE("Can't open %s\n(%s)\n", MISC_NAME, strerror(errno));
156 return -1;
157 }
158 if (mtd_write_data(write, data, size) != size) {
159 LOGE("Can't write %s\n(%s)\n", MISC_NAME, strerror(errno));
160 mtd_write_close(write);
161 return -1;
162 }
163 if (mtd_write_close(write)) {
164 LOGE("Can't finish %s\n(%s)\n", MISC_NAME, strerror(errno));
165 return -1;
166 }
167
168 LOGI("Set boot command \"%s\"\n", in->command[0] != 255 ? in->command : "");
169 return 0;
170}
Doug Zongker04611da2010-08-12 15:35:29 -0700171
172#endif