config_mbr.c revision 01dda204cd28fe181691b4a44a51be7e5666d0c8
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    ALOGI("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        ALOGE("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        ALOGE("Maximum number of primary partition exceeded.");
79        return NULL;
80    }
81
82    if (!(item = alloc_wl(sizeof(struct pc_partition)))) {
83        ALOGE("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        ALOGE("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            ALOGE("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                    ALOGE("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            ALOGE("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            ALOGE("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    ALOGE("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        ALOGE("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        ALOGE("Device name is too long?!");
320        free(dev_name);
321        return NULL;
322    }
323
324    return dev_name;
325}
326