summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSan Mehat2010-03-10 14:46:00 -0600
committerSan Mehat2010-03-10 17:19:06 -0600
commita6391f1006b961ca89d1c79a826375380684a4de (patch)
tree50f5a2b2179d52cf24cb6ad09c4842d0404610e2 /libdiskconfig
parent732799eca9f2767996bc423503937c4c935bb630 (diff)
downloadplatform-system-core-a6391f1006b961ca89d1c79a826375380684a4de.tar.gz
platform-system-core-a6391f1006b961ca89d1c79a826375380684a4de.tar.xz
platform-system-core-a6391f1006b961ca89d1c79a826375380684a4de.zip
system: libdiskconfig: Add libdiskconfig
Change-Id: Ie7a7b5d8016dec60cdfb17228c3f519789c98564 Signed-off-by: San Mehat <san@google.com>
Diffstat (limited to 'libdiskconfig')
-rw-r--r--libdiskconfig/Android.mk20
-rw-r--r--libdiskconfig/config_mbr.c325
-rw-r--r--libdiskconfig/diskconfig.c536
-rw-r--r--libdiskconfig/diskutils.c117
-rw-r--r--libdiskconfig/dump_diskconfig.c42
-rw-r--r--libdiskconfig/write_lst.c92
6 files changed, 1132 insertions, 0 deletions
diff --git a/libdiskconfig/Android.mk b/libdiskconfig/Android.mk
new file mode 100644
index 000000000..f26410315
--- /dev/null
+++ b/libdiskconfig/Android.mk
@@ -0,0 +1,20 @@
1LOCAL_PATH := $(call my-dir)
2include $(CLEAR_VARS)
3
4ifneq ($(TARGET_SIMULATOR),true)
5
6include $(CLEAR_VARS)
7
8LOCAL_SRC_FILES := \
9 diskconfig.c \
10 diskutils.c \
11 write_lst.c \
12 config_mbr.c
13
14LOCAL_MODULE := libdiskconfig
15LOCAL_PRELINK_MODULE := false
16LOCAL_SYSTEM_SHARED_LIBRARIES := libcutils liblog libc
17
18include $(BUILD_SHARED_LIBRARY)
19
20endif # ! TARGET_SIMULATOR
diff --git a/libdiskconfig/config_mbr.c b/libdiskconfig/config_mbr.c
new file mode 100644
index 000000000..825ba6029
--- /dev/null
+++ b/libdiskconfig/config_mbr.c
@@ -0,0 +1,325 @@
1/* libs/diskconfig/diskconfig.c
2 *
3 * Copyright 2008, The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#define LOG_TAG "config_mbr"
19#include <stdint.h>
20#include <stdlib.h>
21#include <string.h>
22#include <stdio.h>
23
24#include <cutils/log.h>
25
26#include <diskconfig/diskconfig.h>
27
28
29/* start and len are in LBA units */
30static void
31cfg_pentry(struct pc_partition *pentry, uint8_t status, uint8_t type,
32 uint32_t start, uint32_t len)
33{
34 if (len > 0) {
35 /* seems that somes BIOSens can get wedged on boot while verifying
36 * the mbr if these are 0 */
37 memset(&pentry->start, 0xff, sizeof(struct chs));
38 memset(&pentry->end, 0xff, sizeof(struct chs));
39 } else {
40 /* zero out the c/h/s entries.. they are not used */
41 memset(&pentry->start, 0, sizeof(struct chs));
42 memset(&pentry->end, 0, sizeof(struct chs));
43 }
44
45 pentry->status = status;
46 pentry->type = type;
47 pentry->start_lba = start;
48 pentry->len_lba = len;
49
50 LOGI("Configuring pentry. status=0x%x type=0x%x start_lba=%u len_lba=%u",
51 pentry->status, pentry->type, pentry->start_lba, pentry->len_lba);
52}
53
54
55static inline uint32_t
56kb_to_lba(uint32_t len_kb, uint32_t sect_size)
57{
58 uint64_t lba;
59
60 lba = (uint64_t)len_kb * 1024;
61 /* bump it up to the next LBA boundary just in case */
62 lba = (lba + (uint64_t)sect_size - 1) & ~((uint64_t)sect_size - 1);
63 lba /= (uint64_t)sect_size;
64 if (lba >= 0xffffffffULL)
65 LOGE("Error converting kb -> lba. 32bit overflow, expect weirdness");
66 return (uint32_t)(lba & 0xffffffffULL);
67}
68
69
70static struct write_list *
71mk_pri_pentry(struct disk_info *dinfo, struct part_info *pinfo, int pnum,
72 uint32_t *lba)
73{
74 struct write_list *item;
75 struct pc_partition *pentry;
76
77 if (pnum >= PC_NUM_BOOT_RECORD_PARTS) {
78 LOGE("Maximum number of primary partition exceeded.");
79 return NULL;
80 }
81
82 if (!(item = alloc_wl(sizeof(struct pc_partition)))) {
83 LOGE("Unable to allocate memory for partition entry.");
84 return NULL;
85 }
86
87 {
88 /* DO NOT DEREFERENCE */
89 struct pc_boot_record *mbr = (void *)PC_MBR_DISK_OFFSET;
90 /* grab the offset in mbr where to write this partition entry. */
91 item->offset = (loff_t)((uint32_t)((uint8_t *)(&mbr->ptable[pnum])));
92 }
93
94 pentry = (struct pc_partition *) &item->data;
95
96 /* need a standard primary partition entry */
97 if (pinfo) {
98 /* need this to be 64 bit in case len_kb is large */
99 uint64_t len_lba;
100
101 if (pinfo->len_kb != (uint32_t)-1) {
102 /* bump it up to the next LBA boundary just in case */
103 len_lba = ((uint64_t)pinfo->len_kb * 1024);
104 len_lba += ((uint64_t)dinfo->sect_size - 1);
105 len_lba &= ~((uint64_t)dinfo->sect_size - 1);
106 len_lba /= (uint64_t)dinfo->sect_size;
107 } else {
108 /* make it fill the rest of disk */
109 len_lba = dinfo->num_lba - *lba;
110 }
111
112 cfg_pentry(pentry, ((pinfo->flags & PART_ACTIVE_FLAG) ?
113 PC_PART_ACTIVE : PC_PART_NORMAL),
114 pinfo->type, *lba, (uint32_t)len_lba);
115
116 pinfo->start_lba = *lba;
117 *lba += (uint32_t)len_lba;
118 } else {
119 /* this should be made an extended partition, and should take
120 * up the rest of the disk as a primary partition */
121 cfg_pentry(pentry, PC_PART_NORMAL, PC_PART_TYPE_EXTENDED,
122 *lba, dinfo->num_lba - *lba);
123
124 /* note that we do not update the *lba because we now have to
125 * create a chain of extended partition tables, and first one is at
126 * *lba */
127 }
128
129 return item;
130}
131
132
133/* This function configures an extended boot record at the beginning of an
134 * extended partition. This creates a logical partition and a pointer to
135 * the next EBR.
136 *
137 * ext_lba == The start of the toplevel extended partition (pointed to by the
138 * entry in the MBR).
139 */
140static struct write_list *
141mk_ext_pentry(struct disk_info *dinfo, struct part_info *pinfo, uint32_t *lba,
142 uint32_t ext_lba, struct part_info *pnext)
143{
144 struct write_list *item;
145 struct pc_boot_record *ebr;
146 uint32_t len; /* in lba units */
147
148 if (!(item = alloc_wl(sizeof(struct pc_boot_record)))) {
149 LOGE("Unable to allocate memory for EBR.");
150 return NULL;
151 }
152
153 /* we are going to write the ebr at the current LBA, and then bump the
154 * lba counter since that is where the logical data partition will start */
155 item->offset = (*lba) * dinfo->sect_size;
156 (*lba)++;
157
158 ebr = (struct pc_boot_record *) &item->data;
159 memset(ebr, 0, sizeof(struct pc_boot_record));
160 ebr->mbr_sig = PC_BIOS_BOOT_SIG;
161
162 if (pinfo->len_kb != (uint32_t)-1)
163 len = kb_to_lba(pinfo->len_kb, dinfo->sect_size);
164 else {
165 if (pnext) {
166 LOGE("Only the last partition can be specified to fill the disk "
167 "(name = '%s')", pinfo->name);
168 goto fail;
169 }
170 len = dinfo->num_lba - *lba;
171 /* update the pinfo structure to reflect the new size, for
172 * bookkeeping */
173 pinfo->len_kb =
174 (uint32_t)(((uint64_t)len * (uint64_t)dinfo->sect_size) /
175 ((uint64_t)1024));
176 }
177
178 cfg_pentry(&ebr->ptable[PC_EBR_LOGICAL_PART], PC_PART_NORMAL,
179 pinfo->type, 1, len);
180
181 pinfo->start_lba = *lba;
182 *lba += len;
183
184 /* If this is not the last partition, we have to create a link to the
185 * next extended partition.
186 *
187 * Otherwise, there's nothing to do since the "pointer entry" is
188 * already zero-filled.
189 */
190 if (pnext) {
191 /* The start lba for next partition is an offset from the beginning
192 * of the top-level extended partition */
193 uint32_t next_start_lba = *lba - ext_lba;
194 uint32_t next_len_lba;
195 if (pnext->len_kb != (uint32_t)-1)
196 next_len_lba = 1 + kb_to_lba(pnext->len_kb, dinfo->sect_size);
197 else
198 next_len_lba = dinfo->num_lba - *lba;
199 cfg_pentry(&ebr->ptable[PC_EBR_NEXT_PTR_PART], PC_PART_NORMAL,
200 PC_PART_TYPE_EXTENDED, next_start_lba, next_len_lba);
201 }
202
203 return item;
204
205fail:
206 free_wl(item);
207 return NULL;
208}
209
210
211struct write_list *
212config_mbr(struct disk_info *dinfo)
213{
214 struct part_info *pinfo;
215 uint32_t cur_lba = dinfo->skip_lba;
216 uint32_t ext_lba = 0;
217 struct write_list *wr_list = NULL;
218 struct write_list *temp_wr = NULL;
219 int cnt = 0;
220 int extended = 0;
221
222 if (!dinfo->part_lst)
223 return NULL;
224
225 for (cnt = 0; cnt < dinfo->num_parts; ++cnt) {
226 pinfo = &dinfo->part_lst[cnt];
227
228 /* Should we create an extedned partition? */
229 if (cnt == (PC_NUM_BOOT_RECORD_PARTS - 1)) {
230 if (cnt + 1 < dinfo->num_parts) {
231 extended = 1;
232 ext_lba = cur_lba;
233 if ((temp_wr = mk_pri_pentry(dinfo, NULL, cnt, &cur_lba)))
234 wlist_add(&wr_list, temp_wr);
235 else {
236 LOGE("Cannot create primary extended partition.");
237 goto fail;
238 }
239 }
240 }
241
242 /* if extended, need 1 lba for ebr */
243 if ((cur_lba + extended) >= dinfo->num_lba)
244 goto nospace;
245 else if (pinfo->len_kb != (uint32_t)-1) {
246 uint32_t sz_lba = (pinfo->len_kb / dinfo->sect_size) * 1024;
247 if ((cur_lba + sz_lba + extended) > dinfo->num_lba)
248 goto nospace;
249 }
250
251 if (!extended)
252 temp_wr = mk_pri_pentry(dinfo, pinfo, cnt, &cur_lba);
253 else {
254 struct part_info *pnext;
255 pnext = cnt + 1 < dinfo->num_parts ? &dinfo->part_lst[cnt+1] : NULL;
256 temp_wr = mk_ext_pentry(dinfo, pinfo, &cur_lba, ext_lba, pnext);
257 }
258
259 if (temp_wr)
260 wlist_add(&wr_list, temp_wr);
261 else {
262 LOGE("Cannot create partition %d (%s).", cnt, pinfo->name);
263 goto fail;
264 }
265 }
266
267 /* fill in the rest of the MBR with empty parts (if needed). */
268 for (; cnt < PC_NUM_BOOT_RECORD_PARTS; ++cnt) {
269 struct part_info blank;
270 cur_lba = 0;
271 memset(&blank, 0, sizeof(struct part_info));
272 if (!(temp_wr = mk_pri_pentry(dinfo, &blank, cnt, &cur_lba))) {
273 LOGE("Cannot create blank partition %d.", cnt);
274 goto fail;
275 }
276 wlist_add(&wr_list, temp_wr);
277 }
278
279 return wr_list;
280
281nospace:
282 LOGE("Not enough space to add parttion '%s'.", pinfo->name);
283
284fail:
285 wlist_free(wr_list);
286 return NULL;
287}
288
289
290/* Returns the device path of the partition referred to by 'name'
291 * Must be freed by the caller.
292 */
293char *
294find_mbr_part(struct disk_info *dinfo, const char *name)
295{
296 struct part_info *plist = dinfo->part_lst;
297 int num = 0;
298 char *dev_name = NULL;
299 int has_extended = (dinfo->num_parts > PC_NUM_BOOT_RECORD_PARTS);
300
301 for(num = 1; num <= dinfo->num_parts; ++num) {
302 if (!strcmp(plist[num-1].name, name))
303 break;
304 }
305
306 if (num > dinfo->num_parts)
307 return NULL;
308
309 if (has_extended && (num >= PC_NUM_BOOT_RECORD_PARTS))
310 num++;
311
312 if (!(dev_name = malloc(MAX_NAME_LEN))) {
313 LOGE("Cannot allocate memory.");
314 return NULL;
315 }
316
317 num = snprintf(dev_name, MAX_NAME_LEN, "%s%d", dinfo->device, num);
318 if (num >= MAX_NAME_LEN) {
319 LOGE("Device name is too long?!");
320 free(dev_name);
321 return NULL;
322 }
323
324 return dev_name;
325}
diff --git a/libdiskconfig/diskconfig.c b/libdiskconfig/diskconfig.c
new file mode 100644
index 000000000..4dd8c5265
--- /dev/null
+++ b/libdiskconfig/diskconfig.c
@@ -0,0 +1,536 @@
1/* libs/diskconfig/diskconfig.c
2 *
3 * Copyright 2008, The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#define LOG_TAG "diskconfig"
19
20#include <errno.h>
21#include <fcntl.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <unistd.h>
26#include <sys/ioctl.h>
27#include <sys/stat.h>
28
29#include <linux/fs.h>
30
31#include <cutils/config_utils.h>
32#include <cutils/log.h>
33
34#include <diskconfig/diskconfig.h>
35
36
37static int
38parse_len(const char *str, uint64_t *plen)
39{
40 char tmp[64];
41 int len_str;
42 uint32_t multiple = 1;
43
44 strncpy(tmp, str, sizeof(tmp));
45 tmp[sizeof(tmp)-1] = '\0';
46 len_str = strlen(tmp);
47 if (!len_str) {
48 LOGE("Invalid disk length specified.");
49 return 1;
50 }
51
52 switch(tmp[len_str - 1]) {
53 case 'M': case 'm':
54 /* megabyte */
55 multiple <<= 10;
56 case 'K': case 'k':
57 /* kilobytes */
58 multiple <<= 10;
59 tmp[len_str - 1] = '\0';
60 break;
61 default:
62 break;
63 }
64
65 *plen = strtoull(tmp, NULL, 0);
66 if (!*plen) {
67 LOGE("Invalid length specified: %s", str);
68 return 1;
69 }
70
71 if (*plen == (uint64_t)-1) {
72 if (multiple > 1) {
73 LOGE("Size modifier illegal when len is -1");
74 return 1;
75 }
76 } else {
77 /* convert len to kilobytes */
78 if (multiple > 1024)
79 multiple >>= 10;
80 *plen *= multiple;
81
82 if (*plen > 0xffffffffULL) {
83 LOGE("Length specified is too large!: %llu KB", *plen);
84 return 1;
85 }
86 }
87
88 return 0;
89}
90
91
92static int
93load_partitions(cnode *root, struct disk_info *dinfo)
94{
95 cnode *partnode;
96
97 dinfo->num_parts = 0;
98 for (partnode = root->first_child; partnode; partnode = partnode->next) {
99 struct part_info *pinfo = &dinfo->part_lst[dinfo->num_parts];
100 const char *tmp;
101
102 /* bleh, i will leak memory here, but i DONT CARE since
103 * the only right thing to do when this function fails
104 * is to quit */
105 pinfo->name = strdup(partnode->name);
106
107 if(config_bool(partnode, "active", 0))
108 pinfo->flags |= PART_ACTIVE_FLAG;
109
110 if (!(tmp = config_str(partnode, "type", NULL))) {
111 LOGE("Partition type required: %s", pinfo->name);
112 return 1;
113 }
114
115 /* possible values are: linux, fat32 */
116 if (!strcmp(tmp, "linux")) {
117 pinfo->type = PC_PART_TYPE_LINUX;
118 } else if (!strcmp(tmp, "fat32")) {
119 pinfo->type = PC_PART_TYPE_FAT32;
120 } else {
121 LOGE("Unsupported partition type found: %s", tmp);
122 return 1;
123 }
124
125 if ((tmp = config_str(partnode, "len", NULL)) != NULL) {
126 uint64_t len;
127 if (parse_len(tmp, &len))
128 return 1;
129 pinfo->len_kb = (uint32_t) len;
130 } else
131 pinfo->len_kb = 0;
132
133 ++dinfo->num_parts;
134 }
135
136 return 0;
137}
138
139struct disk_info *
140load_diskconfig(const char *fn, char *path_override)
141{
142 struct disk_info *dinfo;
143 cnode *devroot;
144 cnode *partnode;
145 cnode *root = config_node("", "");
146 const char *tmp;
147
148 if (!(dinfo = malloc(sizeof(struct disk_info)))) {
149 LOGE("Could not malloc disk_info");
150 return NULL;
151 }
152 memset(dinfo, 0, sizeof(struct disk_info));
153
154 if (!(dinfo->part_lst = malloc(MAX_NUM_PARTS * sizeof(struct part_info)))) {
155 LOGE("Could not malloc part_lst");
156 goto fail;
157 }
158 memset(dinfo->part_lst, 0,
159 (MAX_NUM_PARTS * sizeof(struct part_info)));
160
161 config_load_file(root, fn);
162 if (root->first_child == NULL) {
163 LOGE("Could not read config file %s", fn);
164 goto fail;
165 }
166
167 if (!(devroot = config_find(root, "device"))) {
168 LOGE("Could not find device section in config file '%s'", fn);
169 goto fail;
170 }
171
172
173 if (!(tmp = config_str(devroot, "path", path_override))) {
174 LOGE("device path is requried");
175 goto fail;
176 }
177 dinfo->device = strdup(tmp);
178
179 /* find the partition scheme */
180 if (!(tmp = config_str(devroot, "scheme", NULL))) {
181 LOGE("partition scheme is required");
182 goto fail;
183 } else if (!strcmp(tmp, "mbr")) {
184 dinfo->scheme = PART_SCHEME_MBR;
185 } else if (!strcmp(tmp, "gpt")) {
186 LOGE("'gpt' partition scheme not supported yet.");
187 goto fail;
188 } else {
189 LOGE("Unknown partition scheme specified: %s", tmp);
190 goto fail;
191 }
192
193 /* grab the sector size (in bytes) */
194 tmp = config_str(devroot, "sector_size", "512");
195 dinfo->sect_size = strtol(tmp, NULL, 0);
196 if (!dinfo->sect_size) {
197 LOGE("Invalid sector size: %s", tmp);
198 goto fail;
199 }
200
201 /* first lba where the partitions will start on disk */
202 if (!(tmp = config_str(devroot, "start_lba", NULL))) {
203 LOGE("start_lba must be provided");
204 goto fail;
205 }
206
207 if (!(dinfo->skip_lba = strtol(tmp, NULL, 0))) {
208 LOGE("Invalid starting LBA (or zero): %s", tmp);
209 goto fail;
210 }
211
212 /* Number of LBAs on disk */
213 if (!(tmp = config_str(devroot, "num_lba", NULL))) {
214 LOGE("num_lba is required");
215 goto fail;
216 }
217 dinfo->num_lba = strtoul(tmp, NULL, 0);
218
219 if (!(partnode = config_find(devroot, "partitions"))) {
220 LOGE("Device must specify partition list");
221 goto fail;
222 }
223
224 if (load_partitions(partnode, dinfo))
225 goto fail;
226
227 return dinfo;
228
229fail:
230 if (dinfo->part_lst)
231 free(dinfo->part_lst);
232 if (dinfo->device)
233 free(dinfo->device);
234 free(dinfo);
235 return NULL;
236}
237
238static int
239sync_ptable(int fd)
240{
241 struct stat stat;
242 int rv;
243
244 sync();
245
246 if (fstat(fd, &stat)) {
247 LOGE("Cannot stat, errno=%d.", errno);
248 return -1;
249 }
250
251 if (S_ISBLK(stat.st_mode) && ((rv = ioctl(fd, BLKRRPART, NULL)) < 0)) {
252 LOGE("Could not re-read partition table. REBOOT!. (errno=%d)", errno);
253 return -1;
254 }
255
256 return 0;
257}
258
259/* This function verifies that the disk info provided is valid, and if so,
260 * returns an open file descriptor.
261 *
262 * This does not necessarily mean that it will later be successfully written
263 * though. If we use the pc-bios partitioning scheme, we must use extended
264 * partitions, which eat up some hd space. If the user manually provisioned
265 * every single partition, but did not account for the extra needed space,
266 * then we will later fail.
267 *
268 * TODO: Make validation more complete.
269 */
270static int
271validate(struct disk_info *dinfo)
272{
273 int fd;
274 int sect_sz;
275 uint64_t disk_size;
276 uint64_t total_size;
277 int cnt;
278 struct stat stat;
279
280 if (!dinfo)
281 return -1;
282
283 if ((fd = open(dinfo->device, O_RDWR)) < 0) {
284 LOGE("Cannot open device '%s' (errno=%d)", dinfo->device, errno);
285 return -1;
286 }
287
288 if (fstat(fd, &stat)) {
289 LOGE("Cannot stat file '%s', errno=%d.", dinfo->device, errno);
290 goto fail;
291 }
292
293
294 /* XXX: Some of the code below is kind of redundant and should probably
295 * be refactored a little, but it will do for now. */
296
297 /* Verify that we can operate on the device that was requested.
298 * We presently only support block devices and regular file images. */
299 if (S_ISBLK(stat.st_mode)) {
300 /* get the sector size and make sure we agree */
301 if (ioctl(fd, BLKSSZGET, &sect_sz) < 0) {
302 LOGE("Cannot get sector size (errno=%d)", errno);
303 goto fail;
304 }
305
306 if (!sect_sz || sect_sz != dinfo->sect_size) {
307 LOGE("Device sector size is zero or sector sizes do not match!");
308 goto fail;
309 }
310
311 /* allow the user override the "disk size" if they provided num_lba */
312 if (!dinfo->num_lba) {
313 if (ioctl(fd, BLKGETSIZE64, &disk_size) < 0) {
314 LOGE("Could not get block device size (errno=%d)", errno);
315 goto fail;
316 }
317 /* XXX: we assume that the disk has < 2^32 sectors :-) */
318 dinfo->num_lba = (uint32_t)(disk_size / (uint64_t)dinfo->sect_size);
319 } else
320 disk_size = (uint64_t)dinfo->num_lba * (uint64_t)dinfo->sect_size;
321 } else if (S_ISREG(stat.st_mode)) {
322 LOGI("Requesting operation on a regular file, not block device.");
323 if (!dinfo->sect_size) {
324 LOGE("Sector size for regular file images cannot be zero");
325 goto fail;
326 }
327 if (dinfo->num_lba)
328 disk_size = (uint64_t)dinfo->num_lba * (uint64_t)dinfo->sect_size;
329 else {
330 dinfo->num_lba = (uint32_t)(stat.st_size / dinfo->sect_size);
331 disk_size = (uint64_t)stat.st_size;
332 }
333 } else {
334 LOGE("Device does not refer to a regular file or a block device!");
335 goto fail;
336 }
337
338#if 1
339 LOGV("Device/file %s: size=%llu bytes, num_lba=%u, sect_size=%d",
340 dinfo->device, disk_size, dinfo->num_lba, dinfo->sect_size);
341#endif
342
343 /* since this is our offset into the disk, we start off with that as
344 * our size of needed partitions */
345 total_size = dinfo->skip_lba * dinfo->sect_size;
346
347 /* add up all the partition sizes and make sure it fits */
348 for (cnt = 0; cnt < dinfo->num_parts; ++cnt) {
349 struct part_info *part = &dinfo->part_lst[cnt];
350 if (part->len_kb != (uint32_t)-1) {
351 total_size += part->len_kb * 1024;
352 } else if (part->len_kb == 0) {
353 LOGE("Zero-size partition '%s' is invalid.", part->name);
354 goto fail;
355 } else {
356 /* the partition requests the rest of the disk. */
357 if (cnt + 1 != dinfo->num_parts) {
358 LOGE("Only the last partition in the list can request to fill "
359 "the rest of disk.");
360 goto fail;
361 }
362 }
363
364 if ((part->type != PC_PART_TYPE_LINUX) &&
365 (part->type != PC_PART_TYPE_FAT32)) {
366 LOGE("Unknown partition type (0x%x) encountered for partition "
367 "'%s'\n", part->type, part->name);
368 goto fail;
369 }
370 }
371
372 /* only matters for disks, not files */
373 if (S_ISBLK(stat.st_mode) && total_size > disk_size) {
374 LOGE("Total requested size of partitions (%llu) is greater than disk "
375 "size (%llu).", total_size, disk_size);
376 goto fail;
377 }
378
379 return fd;
380
381fail:
382 close(fd);
383 return -1;
384}
385
386static int
387validate_and_config(struct disk_info *dinfo, int *fd, struct write_list **lst)
388{
389 *lst = NULL;
390 *fd = -1;
391
392 if ((*fd = validate(dinfo)) < 0)
393 return 1;
394
395 switch (dinfo->scheme) {
396 case PART_SCHEME_MBR:
397 *lst = config_mbr(dinfo);
398 return *lst == NULL;
399 case PART_SCHEME_GPT:
400 /* not supported yet */
401 default:
402 LOGE("Uknown partition scheme.");
403 break;
404 }
405
406 close(*fd);
407 *lst = NULL;
408 return 1;
409}
410
411/* validate and process the disk layout configuration.
412 * This will cause an update to the partitions' start lba.
413 *
414 * Basically, this does the same thing as apply_disk_config in test mode,
415 * except that wlist_commit is not called to print out the data to be
416 * written.
417 */
418int
419process_disk_config(struct disk_info *dinfo)
420{
421 struct write_list *lst;
422 int fd;
423
424 if (validate_and_config(dinfo, &fd, &lst) != 0)
425 return 1;
426
427 close(fd);
428 wlist_free(lst);
429 return 0;
430}
431
432
433int
434apply_disk_config(struct disk_info *dinfo, int test)
435{
436 int fd;
437 struct write_list *wr_lst = NULL;
438 int rv;
439
440 if (validate_and_config(dinfo, &fd, &wr_lst) != 0) {
441 LOGE("Configuration is invalid.");
442 goto fail;
443 }
444
445 if ((rv = wlist_commit(fd, wr_lst, test)) >= 0)
446 rv = test ? 0 : sync_ptable(fd);
447
448 close(fd);
449 wlist_free(wr_lst);
450 return rv;
451
452fail:
453 close(fd);
454 if (wr_lst)
455 wlist_free(wr_lst);
456 return 1;
457}
458
459int
460dump_disk_config(struct disk_info *dinfo)
461{
462 int cnt;
463 struct part_info *part;
464
465 printf("Device: %s\n", dinfo->device);
466 printf("Scheme: ");
467 switch (dinfo->scheme) {
468 case PART_SCHEME_MBR:
469 printf("MBR");
470 break;
471 case PART_SCHEME_GPT:
472 printf("GPT (unsupported)");
473 break;
474 default:
475 printf("Unknown");
476 break;
477 }
478 printf ("\n");
479
480 printf("Sector size: %d\n", dinfo->sect_size);
481 printf("Skip leading LBAs: %u\n", dinfo->skip_lba);
482 printf("Number of LBAs: %u\n", dinfo->num_lba);
483 printf("Partitions:\n");
484
485 for (cnt = 0; cnt < dinfo->num_parts; ++cnt) {
486 part = &dinfo->part_lst[cnt];
487 printf("\tname = %s\n", part->name);
488 printf("\t\tflags = %s\n",
489 part->flags & PART_ACTIVE_FLAG ? "Active" : "None");
490 printf("\t\ttype = %s\n",
491 part->type == PC_PART_TYPE_LINUX ? "Linux" : "Unknown");
492 if (part->len_kb == (uint32_t)-1)
493 printf("\t\tlen = rest of disk\n");
494 else
495 printf("\t\tlen = %uKB\n", part->len_kb);
496 }
497 printf("Total number of partitions: %d\n", cnt);
498 printf("\n");
499
500 return 0;
501}
502
503struct part_info *
504find_part(struct disk_info *dinfo, const char *name)
505{
506 struct part_info *pinfo;
507 int cnt;
508
509 for (cnt = 0; cnt < dinfo->num_parts; ++cnt) {
510 pinfo = &dinfo->part_lst[cnt];
511 if (!strcmp(pinfo->name, name))
512 return pinfo;
513 }
514
515 return NULL;
516}
517
518/* NOTE: If the returned ptr is non-NULL, it must be freed by the caller. */
519char *
520find_part_device(struct disk_info *dinfo, const char *name)
521{
522 switch (dinfo->scheme) {
523 case PART_SCHEME_MBR:
524 return find_mbr_part(dinfo, name);
525 case PART_SCHEME_GPT:
526 LOGE("GPT is presently not supported");
527 break;
528 default:
529 LOGE("Unknown partition table scheme");
530 break;
531 }
532
533 return NULL;
534}
535
536
diff --git a/libdiskconfig/diskutils.c b/libdiskconfig/diskutils.c
new file mode 100644
index 000000000..22767c00e
--- /dev/null
+++ b/libdiskconfig/diskutils.c
@@ -0,0 +1,117 @@
1/* libs/diskconfig/diskutils.c
2 *
3 * Copyright 2008, The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#define LOG_TAG "diskutils"
19
20#include <errno.h>
21#include <fcntl.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <unistd.h>
26#include <sys/stat.h>
27
28#include <cutils/log.h>
29
30#include <diskconfig/diskconfig.h>
31
32int
33write_raw_image(const char *dst, const char *src, loff_t offset, int test)
34{
35 int dst_fd = -1;
36 int src_fd = -1;
37 uint8_t buffer[2048];
38 int nr_bytes;
39 int tmp;
40 int done = 0;
41 uint64_t total = 0;
42
43 LOGI("Writing RAW image '%s' to '%s' (offset=%llu)", src, dst, offset);
44 if ((src_fd = open(src, O_RDONLY)) < 0) {
45 LOGE("Could not open %s for reading (errno=%d).", src, errno);
46 goto fail;
47 }
48
49 if (!test) {
50 if ((dst_fd = open(dst, O_RDWR)) < 0) {
51 LOGE("Could not open '%s' for read/write (errno=%d).", dst, errno);
52 goto fail;
53 }
54
55 if (lseek64(dst_fd, offset, SEEK_SET) != offset) {
56 LOGE("Could not seek to offset %lld in %s.", offset, dst);
57 goto fail;
58 }
59 }
60
61 while (!done) {
62 if ((nr_bytes = read(src_fd, buffer, sizeof(buffer))) < 0) {
63 /* XXX: Should we not even bother with EINTR? */
64 if (errno == EINTR)
65 continue;
66 LOGE("Error (%d) while reading from '%s'", errno, src);
67 goto fail;
68 }
69
70 if (!nr_bytes) {
71 /* we're done. */
72 done = 1;
73 break;
74 }
75
76 total += nr_bytes;
77
78 /* skip the write loop if we're testing */
79 if (test)
80 nr_bytes = 0;
81
82 while (nr_bytes > 0) {
83 if ((tmp = write(dst_fd, buffer, nr_bytes)) < 0) {
84 /* XXX: Should we not even bother with EINTR? */
85 if (errno == EINTR)
86 continue;
87 LOGE("Error (%d) while writing to '%s'", errno, dst);
88 goto fail;
89 }
90 if (!tmp)
91 continue;
92 nr_bytes -= tmp;
93 }
94 }
95
96 if (!done) {
97 LOGE("Exited read/write loop without setting flag! WTF?!");
98 goto fail;
99 }
100
101 if (dst_fd >= 0)
102 fsync(dst_fd);
103
104 LOGI("Wrote %llu bytes to %s @ %lld", total, dst, offset);
105
106 close(src_fd);
107 if (dst_fd >= 0)
108 close(dst_fd);
109 return 0;
110
111fail:
112 if (dst_fd >= 0)
113 close(dst_fd);
114 if (src_fd >= 0)
115 close(src_fd);
116 return 1;
117}
diff --git a/libdiskconfig/dump_diskconfig.c b/libdiskconfig/dump_diskconfig.c
new file mode 100644
index 000000000..fff19f5ab
--- /dev/null
+++ b/libdiskconfig/dump_diskconfig.c
@@ -0,0 +1,42 @@
1/* libs/diskconfig/dump_diskconfig.c
2 *
3 * Copyright 2008, The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#define LOG_TAG "dump_diskconfig"
19#include <stdio.h>
20
21#include <cutils/log.h>
22
23#include "diskconfig.h"
24
25int
26main(int argc, char *argv[])
27{
28 struct disk_info *dinfo;
29
30 if (argc < 2) {
31 LOGE("usage: %s <conf file>", argv[0]);
32 return 1;
33 }
34
35 if (!(dinfo = load_diskconfig(argv[1], NULL)))
36 return 1;
37
38 dump_disk_config(dinfo);
39
40 return 0;
41}
42
diff --git a/libdiskconfig/write_lst.c b/libdiskconfig/write_lst.c
new file mode 100644
index 000000000..12b7cd775
--- /dev/null
+++ b/libdiskconfig/write_lst.c
@@ -0,0 +1,92 @@
1/* libs/diskconfig/write_lst.c
2 *
3 * Copyright 2008, The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#define LOG_TAG "write_lst"
19#include <sys/types.h>
20#include <stdint.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <unistd.h>
24
25#include <cutils/log.h>
26
27#include <diskconfig/diskconfig.h>
28
29struct write_list *
30alloc_wl(uint32_t data_len)
31{
32 struct write_list *item;
33
34 if (!(item = malloc(sizeof(struct write_list) + data_len))) {
35 LOGE("Unable to allocate memory.");
36 return NULL;
37 }
38
39 item->len = data_len;
40 return item;
41}
42
43void
44free_wl(struct write_list *item)
45{
46 if (item)
47 free(item);
48}
49
50struct write_list *
51wlist_add(struct write_list **lst, struct write_list *item)
52{
53 item->next = (*lst);
54 *lst = item;
55 return item;
56}
57
58void
59wlist_free(struct write_list *lst)
60{
61 struct write_list *temp_wr;
62 while (lst) {
63 temp_wr = lst->next;
64 free_wl(lst);
65 lst = temp_wr;
66 }
67}
68
69int
70wlist_commit(int fd, struct write_list *lst, int test)
71{
72 for(; lst; lst = lst->next) {
73 if (lseek64(fd, lst->offset, SEEK_SET) != (loff_t)lst->offset) {
74 LOGE("Cannot seek to the specified position (%lld).", lst->offset);
75 goto fail;
76 }
77
78 if (!test) {
79 if (write(fd, lst->data, lst->len) != (int)lst->len) {
80 LOGE("Failed writing %u bytes at position %lld.", lst->len,
81 lst->offset);
82 goto fail;
83 }
84 } else
85 LOGI("Would write %d bytes @ offset %lld.", lst->len, lst->offset);
86 }
87
88 return 0;
89
90fail:
91 return -1;
92}