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