blob: 38b5651bfea220b07be9e9cfb040cf6d46216ef3 [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:";
28static const int MISC_PAGES = 3; // number of pages to save
29static const int MISC_COMMAND_PAGE = 1; // bootloader command is this page
30
31#ifdef LOG_VERBOSE
32static void dump_data(const char *data, int len) {
33 int pos;
34 for (pos = 0; pos < len; ) {
35 printf("%05x: %02x", pos, data[pos]);
36 for (++pos; pos < len && (pos % 24) != 0; ++pos) {
37 printf(" %02x", data[pos]);
38 }
39 printf("\n");
40 }
41}
42#endif
43
44int get_bootloader_message(struct bootloader_message *out) {
45 size_t write_size;
46 const MtdPartition *part = get_root_mtd_partition(MISC_NAME);
47 if (part == NULL || mtd_partition_info(part, NULL, NULL, &write_size)) {
48 LOGE("Can't find %s\n", MISC_NAME);
49 return -1;
50 }
51
52 MtdReadContext *read = mtd_read_partition(part);
53 if (read == NULL) {
54 LOGE("Can't open %s\n(%s)\n", MISC_NAME, strerror(errno));
55 return -1;
56 }
57
58 const ssize_t size = write_size * MISC_PAGES;
59 char data[size];
60 ssize_t r = mtd_read_data(read, data, size);
61 if (r != size) LOGE("Can't read %s\n(%s)\n", MISC_NAME, strerror(errno));
62 mtd_read_close(read);
63 if (r != size) return -1;
64
65#ifdef LOG_VERBOSE
66 printf("\n--- get_bootloader_message ---\n");
67 dump_data(data, size);
68 printf("\n");
69#endif
70
71 memcpy(out, &data[write_size * MISC_COMMAND_PAGE], sizeof(*out));
72 return 0;
73}
74
75int set_bootloader_message(const struct bootloader_message *in) {
76 size_t write_size;
77 const MtdPartition *part = get_root_mtd_partition(MISC_NAME);
78 if (part == NULL || mtd_partition_info(part, NULL, NULL, &write_size)) {
79 LOGE("Can't find %s\n", MISC_NAME);
80 return -1;
81 }
82
83 MtdReadContext *read = mtd_read_partition(part);
84 if (read == NULL) {
85 LOGE("Can't open %s\n(%s)\n", MISC_NAME, strerror(errno));
86 return -1;
87 }
88
89 ssize_t size = write_size * MISC_PAGES;
90 char data[size];
91 ssize_t r = mtd_read_data(read, data, size);
92 if (r != size) LOGE("Can't read %s\n(%s)\n", MISC_NAME, strerror(errno));
93 mtd_read_close(read);
94 if (r != size) return -1;
95
96 memcpy(&data[write_size * MISC_COMMAND_PAGE], in, sizeof(*in));
97
98#ifdef LOG_VERBOSE
99 printf("\n--- set_bootloader_message ---\n");
100 dump_data(data, size);
101 printf("\n");
102#endif
103
104 MtdWriteContext *write = mtd_write_partition(part);
105 if (write == NULL) {
106 LOGE("Can't open %s\n(%s)\n", MISC_NAME, strerror(errno));
107 return -1;
108 }
109 if (mtd_write_data(write, data, size) != size) {
110 LOGE("Can't write %s\n(%s)\n", MISC_NAME, strerror(errno));
111 mtd_write_close(write);
112 return -1;
113 }
114 if (mtd_write_close(write)) {
115 LOGE("Can't finish %s\n(%s)\n", MISC_NAME, strerror(errno));
116 return -1;
117 }
118
119 LOGI("Set boot command \"%s\"\n", in->command[0] != 255 ? in->command : "");
120 return 0;
121}