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"; |
| 30 | |
| 31 | int format_partition(blkdev_t *part) |
| 32 | { |
| 33 | char *devpath; |
| 34 | char *args[7]; |
| 35 | |
| 36 | devpath = blkdev_get_devpath(part); |
| 37 | |
| 38 | args[0] = MKDOSFS_PATH; |
| 39 | args[1] = "-F 32"; |
| 40 | args[2] = "-c 32"; |
| 41 | args[3] = "-n 2"; |
| 42 | args[4] = "-O android"; |
| 43 | args[5] = devpath; |
| 44 | args[6] = NULL; |
| 45 | |
| 46 | int rc = logwrap(6, args); |
| 47 | |
| 48 | free(devpath); |
| 49 | |
| 50 | if (rc == 0) { |
| 51 | LOG_VOL("Filesystem formatted OK\n"); |
| 52 | return 0; |
| 53 | } else { |
| 54 | LOGE("Format failed (unknokwn exit code %d)\n", rc); |
| 55 | return -EIO; |
| 56 | } |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | int initialize_mbr(blkdev_t *disk) |
| 61 | { |
| 62 | int fd, rc; |
| 63 | unsigned char block[512]; |
| 64 | struct dos_partition part; |
| 65 | char *devpath; |
| 66 | |
| 67 | devpath = blkdev_get_devpath(disk); |
| 68 | |
| 69 | memset(&part, 0, sizeof(part)); |
| 70 | part.dp_flag = 0x80; |
| 71 | part.dp_typ = 0xc; |
| 72 | part.dp_start = ((1024 * 64) / 512) + 1; |
| 73 | part.dp_size = disk->nr_sec - part.dp_start; |
| 74 | |
| 75 | memset(block, 0, sizeof(block)); |
| 76 | block[0x1fe] = 0x55; |
| 77 | block[0x1ff] = 0xaa; |
| 78 | |
| 79 | dos_partition_enc(block + DOSPARTOFF, &part); |
| 80 | |
| 81 | if ((fd = open(devpath, O_RDWR)) < 0) { |
| 82 | LOGE("Error opening disk file (%s)\n", strerror(errno)); |
| 83 | return -errno; |
| 84 | } |
| 85 | free(devpath); |
| 86 | |
| 87 | if (write(fd, block, sizeof(block)) < 0) { |
| 88 | LOGE("Error writing MBR (%s)\n", strerror(errno)); |
| 89 | close(fd); |
| 90 | return -errno; |
| 91 | } |
| 92 | |
| 93 | if (ioctl(fd, BLKRRPART, NULL) < 0) { |
| 94 | LOGE("Error re-reading partition table (%s)\n", strerror(errno)); |
| 95 | close(fd); |
| 96 | return -errno; |
| 97 | } |
| 98 | close(fd); |
| 99 | return 0; |
| 100 | } |