The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2008 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 <stdlib.h> |
| 18 | #include <stdio.h> |
| 19 | #include <unistd.h> |
| 20 | #include <string.h> |
| 21 | #include <fcntl.h> |
| 22 | #include <sys/mount.h> |
| 23 | #include <errno.h> |
| 24 | |
| 25 | #include "sysdeps.h" |
| 26 | |
| 27 | #define TRACE_TAG TRACE_ADB |
| 28 | #include "adb.h" |
| 29 | |
| 30 | |
| 31 | static int system_ro = 1; |
| 32 | |
| 33 | /* Returns the mount number of the requested partition from /proc/mtd */ |
| 34 | static int find_mount(const char *findme) |
| 35 | { |
| 36 | int fd; |
| 37 | int res; |
| 38 | int size; |
| 39 | char *token = NULL; |
| 40 | const char delims[] = "\n"; |
| 41 | char buf[1024]; |
| 42 | |
| 43 | fd = unix_open("/proc/mtd", O_RDONLY); |
| 44 | if (fd < 0) |
| 45 | return -errno; |
| 46 | |
| 47 | buf[sizeof(buf) - 1] = '\0'; |
| 48 | size = adb_read(fd, buf, sizeof(buf) - 1); |
| 49 | adb_close(fd); |
| 50 | |
| 51 | token = strtok(buf, delims); |
| 52 | |
| 53 | while (token) { |
| 54 | char mtdname[16]; |
| 55 | int mtdnum, mtdsize, mtderasesize; |
| 56 | |
| 57 | res = sscanf(token, "mtd%d: %x %x %15s", |
| 58 | &mtdnum, &mtdsize, &mtderasesize, mtdname); |
| 59 | |
| 60 | if (res == 4 && !strcmp(mtdname, findme)) |
| 61 | return mtdnum; |
| 62 | |
| 63 | token = strtok(NULL, delims); |
| 64 | } |
| 65 | return -1; |
| 66 | } |
| 67 | |
| 68 | /* Init mounts /system as read only, remount to enable writes. */ |
| 69 | static int remount_system() |
| 70 | { |
| 71 | int num; |
| 72 | char source[64]; |
| 73 | if (system_ro == 0) { |
| 74 | return 0; |
| 75 | } |
| 76 | if ((num = find_mount("\"system\"")) < 0) |
| 77 | return -1; |
| 78 | |
| 79 | snprintf(source, sizeof source, "/dev/block/mtdblock%d", num); |
| 80 | system_ro = mount(source, "/system", "yaffs2", MS_REMOUNT, NULL); |
| 81 | return system_ro; |
| 82 | } |
| 83 | |
| 84 | static void write_string(int fd, const char* str) |
| 85 | { |
| 86 | writex(fd, str, strlen(str)); |
| 87 | } |
| 88 | |
| 89 | void remount_service(int fd, void *cookie) |
| 90 | { |
| 91 | int ret = remount_system(); |
| 92 | |
| 93 | if (!ret) |
| 94 | write_string(fd, "remount succeeded\n"); |
| 95 | else { |
| 96 | char buffer[200]; |
| 97 | snprintf(buffer, sizeof(buffer), "remount failed: %s\n", strerror(errno)); |
| 98 | write_string(fd, buffer); |
| 99 | } |
| 100 | |
| 101 | adb_close(fd); |
| 102 | } |
| 103 | |