blob: dd0515cf7423fc93a0e6e462cb2f0845f297b142 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001
2/*
3 * Copyright (C) 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#include <fcntl.h>
19#include <errno.h>
20
21#include <linux/fs.h>
22
23#include "vold.h"
24#include "blkdev.h"
25#include "format.h"
26#include "diskmbr.h"
27#include "logwrapper.h"
28
29static char MKDOSFS_PATH[] = "/system/bin/mkdosfs";
30static char MKE2FS_PATH[] = "/system/bin/mke2fs";
31
32int format_partition(blkdev_t *part, char *type)
33{
34 char *devpath;
35 int rc = -EINVAL;
36
37 devpath = blkdev_get_devpath(part);
38
39 if (!strcmp(type, FORMAT_TYPE_FAT32)) {
40 char *args[7];
41 args[0] = MKDOSFS_PATH;
42 args[1] = "-F 32";
43 args[2] = "-c 32";
44 args[3] = "-n 2";
45 args[4] = "-O android";
46 args[5] = devpath;
47 args[6] = NULL;
48 rc = logwrap(6, args);
49 } else {
50 char *args[7];
51 args[0] = MKE2FS_PATH;
52 args[1] = "-b 4096";
53 args[2] = "-m 1";
54 args[3] = "-L android";
55 args[4] = "-v";
56 args[5] = devpath;
57 args[6] = NULL;
58 rc = logwrap(6, args);
59 }
60
61 free(devpath);
62
63 if (rc == 0) {
64 LOG_VOL("Filesystem formatted OK");
65 return 0;
66 } else {
67 LOGE("Format failed (unknokwn exit code %d)", rc);
68 return -EIO;
69 }
70 return 0;
71}
72
73int initialize_mbr(blkdev_t *disk)
74{
75 int fd, rc;
76 unsigned char block[512];
77 struct dos_partition part;
78 char *devpath;
79
80 devpath = blkdev_get_devpath(disk);
81
82 memset(&part, 0, sizeof(part));
83 part.dp_flag = 0x80;
84 part.dp_typ = 0xc;
85 part.dp_start = ((1024 * 64) / 512) + 1;
86 part.dp_size = disk->nr_sec - part.dp_start;
87
88 memset(block, 0, sizeof(block));
89 block[0x1fe] = 0x55;
90 block[0x1ff] = 0xaa;
91
92 dos_partition_enc(block + DOSPARTOFF, &part);
93
94 if ((fd = open(devpath, O_RDWR)) < 0) {
95 LOGE("Error opening disk file (%s)", strerror(errno));
96 return -errno;
97 }
98 free(devpath);
99
100 if (write(fd, block, sizeof(block)) < 0) {
101 LOGE("Error writing MBR (%s)", strerror(errno));
102 close(fd);
103 return -errno;
104 }
105
106 if (ioctl(fd, BLKRRPART, NULL) < 0) {
107 LOGE("Error re-reading partition table (%s)", strerror(errno));
108 close(fd);
109 return -errno;
110 }
111 close(fd);
112 return 0;
113}