blob: dd0515cf7423fc93a0e6e462cb2f0845f297b142 [file] [log] [blame]
The Android Open Source Project13f797d2009-02-10 15:44:07 -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";
The Android Open Source Project1b8e5a62009-02-13 12:57:54 -080030static char MKE2FS_PATH[] = "/system/bin/mke2fs";
The Android Open Source Project13f797d2009-02-10 15:44:07 -080031
The Android Open Source Project1b8e5a62009-02-13 12:57:54 -080032int format_partition(blkdev_t *part, char *type)
The Android Open Source Project13f797d2009-02-10 15:44:07 -080033{
34 char *devpath;
The Android Open Source Project1b8e5a62009-02-13 12:57:54 -080035 int rc = -EINVAL;
The Android Open Source Project13f797d2009-02-10 15:44:07 -080036
37 devpath = blkdev_get_devpath(part);
38
The Android Open Source Project1b8e5a62009-02-13 12:57:54 -080039 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 }
The Android Open Source Project13f797d2009-02-10 15:44:07 -080060
61 free(devpath);
62
63 if (rc == 0) {
The Android Open Source Project1b8e5a62009-02-13 12:57:54 -080064 LOG_VOL("Filesystem formatted OK");
The Android Open Source Project13f797d2009-02-10 15:44:07 -080065 return 0;
66 } else {
The Android Open Source Project1b8e5a62009-02-13 12:57:54 -080067 LOGE("Format failed (unknokwn exit code %d)", rc);
The Android Open Source Project13f797d2009-02-10 15:44:07 -080068 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) {
The Android Open Source Project1b8e5a62009-02-13 12:57:54 -080095 LOGE("Error opening disk file (%s)", strerror(errno));
The Android Open Source Project13f797d2009-02-10 15:44:07 -080096 return -errno;
97 }
98 free(devpath);
99
100 if (write(fd, block, sizeof(block)) < 0) {
The Android Open Source Project1b8e5a62009-02-13 12:57:54 -0800101 LOGE("Error writing MBR (%s)", strerror(errno));
The Android Open Source Project13f797d2009-02-10 15:44:07 -0800102 close(fd);
103 return -errno;
104 }
105
106 if (ioctl(fd, BLKRRPART, NULL) < 0) {
The Android Open Source Project1b8e5a62009-02-13 12:57:54 -0800107 LOGE("Error re-reading partition table (%s)", strerror(errno));
The Android Open Source Project13f797d2009-02-10 15:44:07 -0800108 close(fd);
109 return -errno;
110 }
111 close(fd);
112 return 0;
113}