blob: cd401972f778c5f3670aa30a6c188befcfab63cd [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
San Mehatb3edd072009-07-06 11:33:45 -070029static char MKDOSFS_PATH[] = "/system/bin/newfs_msdos";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080030static 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)) {
San Mehatb3edd072009-07-06 11:33:45 -070040 char *args[9];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080041 args[0] = MKDOSFS_PATH;
San Mehatb3edd072009-07-06 11:33:45 -070042 args[1] = "-F";
43 args[2] = "32";
44 args[3] = "-c";
45 args[4] = "16";
46 args[5] = "-O";
47 args[6] = "android";
48 args[7] = devpath;
49 args[8] = NULL;
50 rc = logwrap(8, args, 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080051 } else {
52 char *args[7];
53 args[0] = MKE2FS_PATH;
54 args[1] = "-b 4096";
55 args[2] = "-m 1";
56 args[3] = "-L android";
57 args[4] = "-v";
58 args[5] = devpath;
59 args[6] = NULL;
San Mehat825c27d2009-06-12 07:39:52 -070060 rc = logwrap(6, args, 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080061 }
62
63 free(devpath);
64
65 if (rc == 0) {
66 LOG_VOL("Filesystem formatted OK");
67 return 0;
68 } else {
69 LOGE("Format failed (unknokwn exit code %d)", rc);
70 return -EIO;
71 }
72 return 0;
73}
74
75int initialize_mbr(blkdev_t *disk)
76{
77 int fd, rc;
78 unsigned char block[512];
79 struct dos_partition part;
80 char *devpath;
81
82 devpath = blkdev_get_devpath(disk);
83
84 memset(&part, 0, sizeof(part));
85 part.dp_flag = 0x80;
86 part.dp_typ = 0xc;
87 part.dp_start = ((1024 * 64) / 512) + 1;
88 part.dp_size = disk->nr_sec - part.dp_start;
89
90 memset(block, 0, sizeof(block));
91 block[0x1fe] = 0x55;
92 block[0x1ff] = 0xaa;
93
94 dos_partition_enc(block + DOSPARTOFF, &part);
95
96 if ((fd = open(devpath, O_RDWR)) < 0) {
97 LOGE("Error opening disk file (%s)", strerror(errno));
98 return -errno;
99 }
100 free(devpath);
101
102 if (write(fd, block, sizeof(block)) < 0) {
103 LOGE("Error writing MBR (%s)", strerror(errno));
104 close(fd);
105 return -errno;
106 }
107
108 if (ioctl(fd, BLKRRPART, NULL) < 0) {
109 LOGE("Error re-reading partition table (%s)", strerror(errno));
110 close(fd);
111 return -errno;
112 }
113 close(fd);
114 return 0;
115}