The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <errno.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <sys/mount.h> |
| 20 | #include <sys/stat.h> |
| 21 | #include <sys/types.h> |
| 22 | #include <unistd.h> |
| 23 | |
| 24 | #include "mtdutils/mtdutils.h" |
| 25 | #include "mtdutils/mounts.h" |
Doug Zongker | 49c73a7 | 2010-06-29 17:36:28 -0700 | [diff] [blame] | 26 | |
| 27 | #ifdef USE_EXT4 |
| 28 | #include "make_ext4fs.h" |
| 29 | #endif |
| 30 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 31 | #include "minzip/Zip.h" |
| 32 | #include "roots.h" |
| 33 | #include "common.h" |
| 34 | |
| 35 | typedef struct { |
| 36 | const char *name; |
| 37 | const char *device; |
| 38 | const char *device2; // If the first one doesn't work (may be NULL) |
| 39 | const char *partition_name; |
| 40 | const char *mount_point; |
| 41 | const char *filesystem; |
| 42 | } RootInfo; |
| 43 | |
| 44 | /* Canonical pointers. |
| 45 | xxx may just want to use enums |
| 46 | */ |
| 47 | static const char g_mtd_device[] = "@\0g_mtd_device"; |
| 48 | static const char g_raw[] = "@\0g_raw"; |
| 49 | static const char g_package_file[] = "@\0g_package_file"; |
Doug Zongker | 23ceeea | 2010-07-08 17:27:55 -0700 | [diff] [blame] | 50 | static const char g_ramdisk[] = "@\0g_ramdisk"; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 51 | |
| 52 | static RootInfo g_roots[] = { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 53 | { "SDCARD:", "/dev/block/mmcblk0p1", "/dev/block/mmcblk0", NULL, "/sdcard", "vfat" }, |
Doug Zongker | 23ceeea | 2010-07-08 17:27:55 -0700 | [diff] [blame] | 54 | { "TMP:", NULL, NULL, NULL, "/tmp", g_ramdisk }, |
Doug Zongker | 49c73a7 | 2010-06-29 17:36:28 -0700 | [diff] [blame] | 55 | |
| 56 | #ifdef USE_EXT4 |
| 57 | { "CACHE:", "/dev/block/platform/sdhci-tegra.3/by-name/cache", NULL, NULL, |
| 58 | "/cache", "ext4" }, |
| 59 | { "DATA:", "/dev/block/platform/sdhci-tegra.3/by-name/userdata", NULL, NULL, |
| 60 | "/data", "ext4" }, |
Doug Zongker | dc9e87c | 2010-07-29 17:08:50 -0700 | [diff] [blame] | 61 | { "EXT:", "/dev/block/sda1", NULL, NULL, "/sdcard", "vfat" }, |
Doug Zongker | 49c73a7 | 2010-06-29 17:36:28 -0700 | [diff] [blame] | 62 | #else |
| 63 | { "CACHE:", g_mtd_device, NULL, "cache", "/cache", "yaffs2" }, |
| 64 | { "DATA:", g_mtd_device, NULL, "userdata", "/data", "yaffs2" }, |
Doug Zongker | dc9e87c | 2010-07-29 17:08:50 -0700 | [diff] [blame] | 65 | { "EXT:", "/dev/block/mmcblk0p1", "/dev/block/mmcblk0", NULL, "/sdcard", "vfat" }, |
Doug Zongker | 04611da | 2010-08-12 15:35:29 -0700 | [diff] [blame] | 66 | { "MISC:", g_mtd_device, NULL, "misc", NULL, g_raw }, |
Doug Zongker | 49c73a7 | 2010-06-29 17:36:28 -0700 | [diff] [blame] | 67 | #endif |
| 68 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 69 | }; |
| 70 | #define NUM_ROOTS (sizeof(g_roots) / sizeof(g_roots[0])) |
| 71 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 72 | static const RootInfo * |
| 73 | get_root_info_for_path(const char *root_path) |
| 74 | { |
| 75 | const char *c; |
| 76 | |
| 77 | /* Find the first colon. |
| 78 | */ |
| 79 | c = root_path; |
| 80 | while (*c != '\0' && *c != ':') { |
| 81 | c++; |
| 82 | } |
| 83 | if (*c == '\0') { |
| 84 | return NULL; |
| 85 | } |
| 86 | size_t len = c - root_path + 1; |
| 87 | size_t i; |
| 88 | for (i = 0; i < NUM_ROOTS; i++) { |
| 89 | RootInfo *info = &g_roots[i]; |
| 90 | if (strncmp(info->name, root_path, len) == 0) { |
| 91 | return info; |
| 92 | } |
| 93 | } |
| 94 | return NULL; |
| 95 | } |
| 96 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 97 | /* Takes a string like "SYSTEM:lib" and turns it into a string |
| 98 | * like "/system/lib". The translated path is put in out_buf, |
| 99 | * and out_buf is returned if the translation succeeded. |
| 100 | */ |
| 101 | const char * |
| 102 | translate_root_path(const char *root_path, char *out_buf, size_t out_buf_len) |
| 103 | { |
| 104 | if (out_buf_len < 1) { |
| 105 | return NULL; |
| 106 | } |
| 107 | |
| 108 | const RootInfo *info = get_root_info_for_path(root_path); |
| 109 | if (info == NULL || info->mount_point == NULL) { |
| 110 | return NULL; |
| 111 | } |
| 112 | |
| 113 | /* Find the relative part of the non-root part of the path. |
| 114 | */ |
| 115 | root_path += strlen(info->name); // strip off the "root:" |
| 116 | while (*root_path != '\0' && *root_path == '/') { |
| 117 | root_path++; |
| 118 | } |
| 119 | |
| 120 | size_t mp_len = strlen(info->mount_point); |
| 121 | size_t rp_len = strlen(root_path); |
| 122 | if (mp_len + 1 + rp_len + 1 > out_buf_len) { |
| 123 | return NULL; |
| 124 | } |
| 125 | |
| 126 | /* Glue the mount point to the relative part of the path. |
| 127 | */ |
| 128 | memcpy(out_buf, info->mount_point, mp_len); |
| 129 | if (out_buf[mp_len - 1] != '/') out_buf[mp_len++] = '/'; |
| 130 | |
| 131 | memcpy(out_buf + mp_len, root_path, rp_len); |
| 132 | out_buf[mp_len + rp_len] = '\0'; |
| 133 | |
| 134 | return out_buf; |
| 135 | } |
| 136 | |
| 137 | static int |
| 138 | internal_root_mounted(const RootInfo *info) |
| 139 | { |
| 140 | if (info->mount_point == NULL) { |
| 141 | return -1; |
| 142 | } |
Doug Zongker | 23ceeea | 2010-07-08 17:27:55 -0700 | [diff] [blame] | 143 | if (info->filesystem == g_ramdisk) { |
| 144 | return 0; |
| 145 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 146 | |
| 147 | /* See if this root is already mounted. |
| 148 | */ |
| 149 | int ret = scan_mounted_volumes(); |
| 150 | if (ret < 0) { |
| 151 | return ret; |
| 152 | } |
| 153 | const MountedVolume *volume; |
| 154 | volume = find_mounted_volume_by_mount_point(info->mount_point); |
| 155 | if (volume != NULL) { |
| 156 | /* It's already mounted. |
| 157 | */ |
| 158 | return 0; |
| 159 | } |
| 160 | return -1; |
| 161 | } |
| 162 | |
| 163 | int |
| 164 | is_root_path_mounted(const char *root_path) |
| 165 | { |
| 166 | const RootInfo *info = get_root_info_for_path(root_path); |
| 167 | if (info == NULL) { |
| 168 | return -1; |
| 169 | } |
| 170 | return internal_root_mounted(info) >= 0; |
| 171 | } |
| 172 | |
| 173 | int |
| 174 | ensure_root_path_mounted(const char *root_path) |
| 175 | { |
| 176 | const RootInfo *info = get_root_info_for_path(root_path); |
| 177 | if (info == NULL) { |
| 178 | return -1; |
| 179 | } |
| 180 | |
| 181 | int ret = internal_root_mounted(info); |
| 182 | if (ret >= 0) { |
| 183 | /* It's already mounted. |
| 184 | */ |
| 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | /* It's not mounted. |
| 189 | */ |
| 190 | if (info->device == g_mtd_device) { |
| 191 | if (info->partition_name == NULL) { |
| 192 | return -1; |
| 193 | } |
| 194 | //TODO: make the mtd stuff scan once when it needs to |
| 195 | mtd_scan_partitions(); |
| 196 | const MtdPartition *partition; |
| 197 | partition = mtd_find_partition_by_name(info->partition_name); |
| 198 | if (partition == NULL) { |
| 199 | return -1; |
| 200 | } |
| 201 | return mtd_mount_partition(partition, info->mount_point, |
| 202 | info->filesystem, 0); |
| 203 | } |
| 204 | |
| 205 | if (info->device == NULL || info->mount_point == NULL || |
| 206 | info->filesystem == NULL || |
| 207 | info->filesystem == g_raw || |
| 208 | info->filesystem == g_package_file) { |
| 209 | return -1; |
| 210 | } |
| 211 | |
| 212 | mkdir(info->mount_point, 0755); // in case it doesn't already exist |
| 213 | if (mount(info->device, info->mount_point, info->filesystem, |
Doug Zongker | 49c73a7 | 2010-06-29 17:36:28 -0700 | [diff] [blame] | 214 | MS_NOATIME | MS_NODEV | MS_NODIRATIME, "")) { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 215 | if (info->device2 == NULL) { |
| 216 | LOGE("Can't mount %s\n(%s)\n", info->device, strerror(errno)); |
| 217 | return -1; |
| 218 | } else if (mount(info->device2, info->mount_point, info->filesystem, |
| 219 | MS_NOATIME | MS_NODEV | MS_NODIRATIME, "")) { |
| 220 | LOGE("Can't mount %s (or %s)\n(%s)\n", |
| 221 | info->device, info->device2, strerror(errno)); |
| 222 | return -1; |
| 223 | } |
| 224 | } |
| 225 | return 0; |
| 226 | } |
| 227 | |
| 228 | int |
| 229 | ensure_root_path_unmounted(const char *root_path) |
| 230 | { |
| 231 | const RootInfo *info = get_root_info_for_path(root_path); |
| 232 | if (info == NULL) { |
| 233 | return -1; |
| 234 | } |
| 235 | if (info->mount_point == NULL) { |
| 236 | /* This root can't be mounted, so by definition it isn't. |
| 237 | */ |
| 238 | return 0; |
| 239 | } |
| 240 | //xxx if TMP: (or similar) just return error |
| 241 | |
| 242 | /* See if this root is already mounted. |
| 243 | */ |
| 244 | int ret = scan_mounted_volumes(); |
| 245 | if (ret < 0) { |
| 246 | return ret; |
| 247 | } |
| 248 | const MountedVolume *volume; |
| 249 | volume = find_mounted_volume_by_mount_point(info->mount_point); |
| 250 | if (volume == NULL) { |
| 251 | /* It's not mounted. |
| 252 | */ |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | return unmount_mounted_volume(volume); |
| 257 | } |
| 258 | |
| 259 | const MtdPartition * |
| 260 | get_root_mtd_partition(const char *root_path) |
| 261 | { |
| 262 | const RootInfo *info = get_root_info_for_path(root_path); |
| 263 | if (info == NULL || info->device != g_mtd_device || |
| 264 | info->partition_name == NULL) |
| 265 | { |
| 266 | return NULL; |
| 267 | } |
| 268 | mtd_scan_partitions(); |
| 269 | return mtd_find_partition_by_name(info->partition_name); |
| 270 | } |
| 271 | |
| 272 | int |
| 273 | format_root_device(const char *root) |
| 274 | { |
| 275 | /* Be a little safer here; require that "root" is just |
| 276 | * a device with no relative path after it. |
| 277 | */ |
| 278 | const char *c = root; |
| 279 | while (*c != '\0' && *c != ':') { |
| 280 | c++; |
| 281 | } |
| 282 | if (c[0] != ':' || c[1] != '\0') { |
| 283 | LOGW("format_root_device: bad root name \"%s\"\n", root); |
| 284 | return -1; |
| 285 | } |
| 286 | |
| 287 | const RootInfo *info = get_root_info_for_path(root); |
| 288 | if (info == NULL || info->device == NULL) { |
| 289 | LOGW("format_root_device: can't resolve \"%s\"\n", root); |
| 290 | return -1; |
| 291 | } |
| 292 | if (info->mount_point != NULL) { |
| 293 | /* Don't try to format a mounted device. |
| 294 | */ |
| 295 | int ret = ensure_root_path_unmounted(root); |
| 296 | if (ret < 0) { |
| 297 | LOGW("format_root_device: can't unmount \"%s\"\n", root); |
| 298 | return ret; |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | /* Format the device. |
| 303 | */ |
| 304 | if (info->device == g_mtd_device) { |
| 305 | mtd_scan_partitions(); |
| 306 | const MtdPartition *partition; |
| 307 | partition = mtd_find_partition_by_name(info->partition_name); |
| 308 | if (partition == NULL) { |
| 309 | LOGW("format_root_device: can't find mtd partition \"%s\"\n", |
| 310 | info->partition_name); |
| 311 | return -1; |
| 312 | } |
| 313 | if (info->filesystem == g_raw || !strcmp(info->filesystem, "yaffs2")) { |
| 314 | MtdWriteContext *write = mtd_write_partition(partition); |
| 315 | if (write == NULL) { |
| 316 | LOGW("format_root_device: can't open \"%s\"\n", root); |
| 317 | return -1; |
| 318 | } else if (mtd_erase_blocks(write, -1) == (off_t) -1) { |
| 319 | LOGW("format_root_device: can't erase \"%s\"\n", root); |
| 320 | mtd_write_close(write); |
| 321 | return -1; |
| 322 | } else if (mtd_write_close(write)) { |
| 323 | LOGW("format_root_device: can't close \"%s\"\n", root); |
| 324 | return -1; |
| 325 | } else { |
| 326 | return 0; |
| 327 | } |
| 328 | } |
| 329 | } |
Doug Zongker | 49c73a7 | 2010-06-29 17:36:28 -0700 | [diff] [blame] | 330 | |
| 331 | #ifdef USE_EXT4 |
| 332 | if (strcmp(info->filesystem, "ext4") == 0) { |
Doug Zongker | 04611da | 2010-08-12 15:35:29 -0700 | [diff] [blame] | 333 | LOGW("starting to reformat ext4\n"); |
Doug Zongker | 49c73a7 | 2010-06-29 17:36:28 -0700 | [diff] [blame] | 334 | reset_ext4fs_info(); |
Ken Sumrall | 8101125 | 2010-08-13 16:08:03 -0700 | [diff] [blame] | 335 | int result = make_ext4fs(info->device, NULL, NULL, 0, 0, 0); |
Doug Zongker | 04611da | 2010-08-12 15:35:29 -0700 | [diff] [blame] | 336 | LOGW("finished reformat ext4: result = %d\n", result); |
Doug Zongker | 49c73a7 | 2010-06-29 17:36:28 -0700 | [diff] [blame] | 337 | if (result != 0) { |
| 338 | LOGW("make_ext4fs failed: %d\n", result); |
| 339 | return -1; |
| 340 | } |
| 341 | return 0; |
| 342 | } |
| 343 | #endif |
| 344 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 345 | //TODO: handle other device types (sdcard, etc.) |
Doug Zongker | 49c73a7 | 2010-06-29 17:36:28 -0700 | [diff] [blame] | 346 | |
| 347 | LOGW("format_root_device: unknown device \"%s\"\n", root); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 348 | return -1; |
| 349 | } |