The Android Open Source Project | 13f797d | 2009-02-10 15:44:07 -0800 | [diff] [blame] | 1 | |
| 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 | |
| 29 | static char MKDOSFS_PATH[] = "/system/bin/mkdosfs"; |
The Android Open Source Project | 1b8e5a6 | 2009-02-13 12:57:54 -0800 | [diff] [blame] | 30 | static char MKE2FS_PATH[] = "/system/bin/mke2fs"; |
The Android Open Source Project | 13f797d | 2009-02-10 15:44:07 -0800 | [diff] [blame] | 31 | |
The Android Open Source Project | 1b8e5a6 | 2009-02-13 12:57:54 -0800 | [diff] [blame] | 32 | int format_partition(blkdev_t *part, char *type) |
The Android Open Source Project | 13f797d | 2009-02-10 15:44:07 -0800 | [diff] [blame] | 33 | { |
| 34 | char *devpath; |
The Android Open Source Project | 1b8e5a6 | 2009-02-13 12:57:54 -0800 | [diff] [blame] | 35 | int rc = -EINVAL; |
The Android Open Source Project | 13f797d | 2009-02-10 15:44:07 -0800 | [diff] [blame] | 36 | |
| 37 | devpath = blkdev_get_devpath(part); |
| 38 | |
The Android Open Source Project | 1b8e5a6 | 2009-02-13 12:57:54 -0800 | [diff] [blame] | 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 | } |
The Android Open Source Project | 13f797d | 2009-02-10 15:44:07 -0800 | [diff] [blame] | 60 | |
| 61 | free(devpath); |
| 62 | |
| 63 | if (rc == 0) { |
The Android Open Source Project | 1b8e5a6 | 2009-02-13 12:57:54 -0800 | [diff] [blame] | 64 | LOG_VOL("Filesystem formatted OK"); |
The Android Open Source Project | 13f797d | 2009-02-10 15:44:07 -0800 | [diff] [blame] | 65 | return 0; |
| 66 | } else { |
The Android Open Source Project | 1b8e5a6 | 2009-02-13 12:57:54 -0800 | [diff] [blame] | 67 | LOGE("Format failed (unknokwn exit code %d)", rc); |
The Android Open Source Project | 13f797d | 2009-02-10 15:44:07 -0800 | [diff] [blame] | 68 | return -EIO; |
| 69 | } |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | int 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 Project | 1b8e5a6 | 2009-02-13 12:57:54 -0800 | [diff] [blame] | 95 | LOGE("Error opening disk file (%s)", strerror(errno)); |
The Android Open Source Project | 13f797d | 2009-02-10 15:44:07 -0800 | [diff] [blame] | 96 | return -errno; |
| 97 | } |
| 98 | free(devpath); |
| 99 | |
| 100 | if (write(fd, block, sizeof(block)) < 0) { |
The Android Open Source Project | 1b8e5a6 | 2009-02-13 12:57:54 -0800 | [diff] [blame] | 101 | LOGE("Error writing MBR (%s)", strerror(errno)); |
The Android Open Source Project | 13f797d | 2009-02-10 15:44:07 -0800 | [diff] [blame] | 102 | close(fd); |
| 103 | return -errno; |
| 104 | } |
| 105 | |
| 106 | if (ioctl(fd, BLKRRPART, NULL) < 0) { |
The Android Open Source Project | 1b8e5a6 | 2009-02-13 12:57:54 -0800 | [diff] [blame] | 107 | LOGE("Error re-reading partition table (%s)", strerror(errno)); |
The Android Open Source Project | 13f797d | 2009-02-10 15:44:07 -0800 | [diff] [blame] | 108 | close(fd); |
| 109 | return -errno; |
| 110 | } |
| 111 | close(fd); |
| 112 | return 0; |
| 113 | } |