diff options
author | The Android Open Source Project <initial-contribution@android.com> | 2009-03-03 19:28:42 -0800 |
---|---|---|
committer | The Android Open Source Project <initial-contribution@android.com> | 2009-03-03 19:28:42 -0800 |
commit | c24a8e688a6312764254beac2b2520bb0c5e998d (patch) | |
tree | 7df23711566b358047301d8413ee399105546e8b /tools | |
parent | ffb48f64fec674c6dd923eb8b1eb3f743e05a8ba (diff) | |
download | bootable_recovery-c24a8e688a6312764254beac2b2520bb0c5e998d.zip bootable_recovery-c24a8e688a6312764254beac2b2520bb0c5e998d.tar.gz bootable_recovery-c24a8e688a6312764254beac2b2520bb0c5e998d.tar.bz2 |
auto import from //depot/cupcake/@135843
Diffstat (limited to 'tools')
-rw-r--r-- | tools/Android.mk | 1 | ||||
-rw-r--r-- | tools/ota/Android.mk | 42 | ||||
-rw-r--r-- | tools/ota/add-property-tag.c | 141 | ||||
-rw-r--r-- | tools/ota/check-lost+found.c | 144 | ||||
-rw-r--r-- | tools/ota/convert-to-bmp.py | 79 | ||||
-rw-r--r-- | tools/ota/make-update-script.c | 228 |
6 files changed, 635 insertions, 0 deletions
diff --git a/tools/Android.mk b/tools/Android.mk new file mode 100644 index 0000000..6571161 --- /dev/null +++ b/tools/Android.mk @@ -0,0 +1 @@ +include $(all-subdir-makefiles) diff --git a/tools/ota/Android.mk b/tools/ota/Android.mk new file mode 100644 index 0000000..b7a57d6 --- /dev/null +++ b/tools/ota/Android.mk @@ -0,0 +1,42 @@ +# Copyright (C) 2008 The Android Open Source Project +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +LOCAL_PATH := $(call my-dir) + +include $(CLEAR_VARS) +LOCAL_MODULE := make-update-script +LOCAL_SRC_FILES := make-update-script.c +include $(BUILD_HOST_EXECUTABLE) + +ifneq ($(TARGET_SIMULATOR),true) + +include $(CLEAR_VARS) +LOCAL_FORCE_STATIC_EXECUTABLE := true +LOCAL_MODULE := add-property-tag +LOCAL_MODULE_PATH := $(TARGET_OUT_OPTIONAL_EXECUTABLES) +LOCAL_MODULE_TAGS := debug +LOCAL_SRC_FILES := add-property-tag.c +LOCAL_STATIC_LIBRARIES := libc +include $(BUILD_EXECUTABLE) + +include $(CLEAR_VARS) +LOCAL_FORCE_STATIC_EXECUTABLE := true +LOCAL_MODULE := check-lost+found +LOCAL_MODULE_PATH := $(TARGET_OUT_OPTIONAL_EXECUTABLES) +LOCAL_MODULE_TAGS := debug +LOCAL_SRC_FILES := check-lost+found.c +LOCAL_STATIC_LIBRARIES := libcutils libc +include $(BUILD_EXECUTABLE) + +endif # !TARGET_SIMULATOR diff --git a/tools/ota/add-property-tag.c b/tools/ota/add-property-tag.c new file mode 100644 index 0000000..5277edd --- /dev/null +++ b/tools/ota/add-property-tag.c @@ -0,0 +1,141 @@ +/* + * Copyright (C) 2008 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <ctype.h> +#include <errno.h> +#include <getopt.h> +#include <limits.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +/* + * Append a tag to a property value in a .prop file if it isn't already there. + * Normally used to modify build properties to record incremental updates. + */ + +// Return nonzero if the tag should be added to this line. +int should_tag(const char *line, const char *propname) { + const char *prop = strstr(line, propname); + if (prop == NULL) return 0; + + // Make sure this is actually the property name (not an accidental hit) + const char *ptr; + for (ptr = line; ptr < prop && isspace(*ptr); ++ptr) ; + if (ptr != prop) return 0; // Must be at the beginning of the line + + for (ptr += strlen(propname); *ptr != '\0' && isspace(*ptr); ++ptr) ; + return (*ptr == '='); // Must be followed by a '=' +} + +// Remove existing tags from the line, return the following number (if any) +int remove_tag(char *line, const char *tag) { + char *pos = strstr(line, tag); + if (pos == NULL) return 0; + + char *end; + int num = strtoul(pos + strlen(tag), &end, 10); + strcpy(pos, end); + return num; +} + +// Write line to output with the tag added, adding a number (if >0) +void write_tagged(FILE *out, const char *line, const char *tag, int number) { + const char *end = line + strlen(line); + while (end > line && isspace(end[-1])) --end; + if (number > 0) { + fprintf(out, "%.*s%s%d%s", end - line, line, tag, number, end); + } else { + fprintf(out, "%.*s%s%s", end - line, line, tag, end); + } +} + +int main(int argc, char **argv) { + const char *filename = "/system/build.prop"; + const char *propname = "ro.build.fingerprint"; + const char *tag = NULL; + int do_remove = 0, do_number = 0; + + int opt; + while ((opt = getopt(argc, argv, "f:p:rn")) != -1) { + switch (opt) { + case 'f': filename = optarg; break; + case 'p': propname = optarg; break; + case 'r': do_remove = 1; break; + case 'n': do_number = 1; break; + case '?': return 2; + } + } + + if (argc != optind + 1) { + fprintf(stderr, + "usage: add-property-tag [flags] tag-to-add\n" + "flags: -f /dir/file.prop (default /system/build.prop)\n" + " -p prop.name (default ro.build.fingerprint)\n" + " -r (if set, remove the tag rather than adding it)\n" + " -n (if set, add and increment a number after the tag)\n"); + return 2; + } + + tag = argv[optind]; + FILE *input = fopen(filename, "r"); + if (input == NULL) { + fprintf(stderr, "can't read %s: %s\n", filename, strerror(errno)); + return 1; + } + + char tmpname[PATH_MAX]; + snprintf(tmpname, sizeof(tmpname), "%s.tmp", filename); + FILE *output = fopen(tmpname, "w"); + if (output == NULL) { + fprintf(stderr, "can't write %s: %s\n", tmpname, strerror(errno)); + return 1; + } + + int found = 0; + char line[4096]; + while (fgets(line, sizeof(line), input)) { + if (!should_tag(line, propname)) { + fputs(line, output); // Pass through unmodified + } else { + found = 1; + int number = remove_tag(line, tag); + if (do_remove) { + fputs(line, output); // Remove the tag but don't re-add it + } else { + write_tagged(output, line, tag, number + do_number); + } + } + } + + fclose(input); + fclose(output); + + if (!found) { + fprintf(stderr, "property %s not found in %s\n", propname, filename); + remove(tmpname); + return 1; + } + + if (rename(tmpname, filename)) { + fprintf(stderr, "can't rename %s to %s: %s\n", + tmpname, filename, strerror(errno)); + remove(tmpname); + return 1; + } + + return 0; +} diff --git a/tools/ota/check-lost+found.c b/tools/ota/check-lost+found.c new file mode 100644 index 0000000..f856275 --- /dev/null +++ b/tools/ota/check-lost+found.c @@ -0,0 +1,144 @@ +/* + * Copyright (C) 2008 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <dirent.h> +#include <errno.h> +#include <fcntl.h> +#include <limits.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/klog.h> +#include <sys/reboot.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <time.h> + +#include "private/android_filesystem_config.h" + +// Sentinel file used to track whether we've forced a reboot +static const char *kMarkerFile = "/data/misc/check-lost+found-rebooted-2"; + +// Output file in tombstones directory (first 8K will be uploaded) +static const char *kOutputDir = "/data/tombstones"; +static const char *kOutputFile = "/data/tombstones/check-lost+found-log"; + +// Partitions to check +static const char *kPartitions[] = { "/system", "/data", "/cache", NULL }; + +/* + * 1. If /data/misc/forced-reboot is missing, touch it & force "unclean" boot. + * 2. Write a log entry with the number of files in lost+found directories. + */ + +int main(int argc, char **argv) { + mkdir(kOutputDir, 0755); + chown(kOutputDir, AID_SYSTEM, AID_SYSTEM); + FILE *out = fopen(kOutputFile, "a"); + if (out == NULL) { + fprintf(stderr, "Can't write %s: %s\n", kOutputFile, strerror(errno)); + return 1; + } + + // Note: only the first 8K of log will be uploaded, so be terse. + time_t start = time(NULL); + fprintf(out, "*** check-lost+found ***\nStarted: %s", ctime(&start)); + + struct stat st; + if (stat(kMarkerFile, &st)) { + // No reboot marker -- need to force an unclean reboot. + // But first, try to create the marker file. If that fails, + // skip the reboot, so we don't get caught in an infinite loop. + + int fd = open(kMarkerFile, O_WRONLY|O_CREAT, 0444); + if (fd >= 0 && close(fd) == 0) { + fprintf(out, "Wrote %s, rebooting\n", kMarkerFile); + fflush(out); + sync(); // Make sure the marker file is committed to disk + + // If possible, dirty each of these partitions before rebooting, + // to make sure the filesystem has to do a scan on mount. + int i; + for (i = 0; kPartitions[i] != NULL; ++i) { + char fn[PATH_MAX]; + snprintf(fn, sizeof(fn), "%s/%s", kPartitions[i], "dirty"); + fd = open(fn, O_WRONLY|O_CREAT, 0444); + if (fd >= 0) { // Don't sweat it if we can't write the file. + write(fd, fn, sizeof(fn)); // write, you know, some data + close(fd); + unlink(fn); + } + } + + reboot(RB_AUTOBOOT); // reboot immediately, with dirty filesystems + fprintf(out, "Reboot failed?!\n"); + exit(1); + } else { + fprintf(out, "Can't write %s: %s\n", kMarkerFile, strerror(errno)); + } + } else { + fprintf(out, "Found %s\n", kMarkerFile); + } + + int i; + for (i = 0; kPartitions[i] != NULL; ++i) { + char fn[PATH_MAX]; + snprintf(fn, sizeof(fn), "%s/%s", kPartitions[i], "lost+found"); + DIR *dir = opendir(fn); + if (dir == NULL) { + fprintf(out, "Can't open %s: %s\n", fn, strerror(errno)); + } else { + int count = 0; + struct dirent *ent; + while ((ent = readdir(dir))) { + if (strcmp(ent->d_name, ".") && strcmp(ent->d_name, "..")) + ++count; + } + closedir(dir); + if (count > 0) { + fprintf(out, "OMGZ FOUND %d FILES IN %s\n", count, fn); + } else { + fprintf(out, "%s is clean\n", fn); + } + } + } + + char dmesg[131073]; + int len = klogctl(KLOG_READ_ALL, dmesg, sizeof(dmesg) - 1); + if (len < 0) { + fprintf(out, "Can't read kernel log: %s\n", strerror(errno)); + } else { // To conserve space, only write lines with certain keywords + fprintf(out, "--- Kernel log ---\n"); + dmesg[len] = '\0'; + char *saveptr, *line; + int in_yaffs = 0; + for (line = strtok_r(dmesg, "\n", &saveptr); line != NULL; + line = strtok_r(NULL, "\n", &saveptr)) { + if (strstr(line, "yaffs: dev is")) in_yaffs = 1; + + if (in_yaffs || + strstr(line, "yaffs") || + strstr(line, "mtd") || + strstr(line, "msm_nand")) { + fprintf(out, "%s\n", line); + } + + if (strstr(line, "yaffs_read_super: isCheckpointed")) in_yaffs = 0; + } + } + + return 0; +} diff --git a/tools/ota/convert-to-bmp.py b/tools/ota/convert-to-bmp.py new file mode 100644 index 0000000..446c09d --- /dev/null +++ b/tools/ota/convert-to-bmp.py @@ -0,0 +1,79 @@ +#!/usr/bin/python2.4 + +"""A simple script to convert asset images to BMP files, that supports +RGBA image.""" + +import struct +import Image +import sys + +infile = sys.argv[1] +outfile = sys.argv[2] + +if not outfile.endswith(".bmp"): + print >> sys.stderr, "Warning: I'm expecting to write BMP files." + +im = Image.open(infile) +if im.mode == 'RGB': + im.save(outfile) +elif im.mode == 'RGBA': + # Python Imaging Library doesn't write RGBA BMP files, so we roll + # our own. + + BMP_HEADER_FMT = ("<" # little-endian + "H" # signature + "L" # file size + "HH" # reserved (set to 0) + "L" # offset to start of bitmap data) + ) + + BITMAPINFO_HEADER_FMT= ("<" # little-endian + "L" # size of this struct + "L" # width + "L" # height + "H" # planes (set to 1) + "H" # bit count + "L" # compression (set to 0 for minui) + "L" # size of image data (0 if uncompressed) + "L" # x pixels per meter (1) + "L" # y pixels per meter (1) + "L" # colors used (0) + "L" # important colors (0) + ) + + fileheadersize = struct.calcsize(BMP_HEADER_FMT) + infoheadersize = struct.calcsize(BITMAPINFO_HEADER_FMT) + + header = struct.pack(BMP_HEADER_FMT, + 0x4d42, # "BM" in little-endian + (fileheadersize + infoheadersize + + im.size[0] * im.size[1] * 4), + 0, 0, + fileheadersize + infoheadersize) + + info = struct.pack(BITMAPINFO_HEADER_FMT, + infoheadersize, + im.size[0], + im.size[1], + 1, + 32, + 0, + 0, + 1, + 1, + 0, + 0) + + f = open(outfile, "wb") + f.write(header) + f.write(info) + data = im.tostring() + for j in range(im.size[1]-1, -1, -1): # rows bottom-to-top + for i in range(j*im.size[0]*4, (j+1)*im.size[0]*4, 4): + f.write(data[i+2]) # B + f.write(data[i+1]) # G + f.write(data[i+0]) # R + f.write(data[i+3]) # A + f.close() +else: + print >> sys.stderr, "Don't know how to handle image mode '%s'." % (im.mode,) diff --git a/tools/ota/make-update-script.c b/tools/ota/make-update-script.c new file mode 100644 index 0000000..225dc52 --- /dev/null +++ b/tools/ota/make-update-script.c @@ -0,0 +1,228 @@ +/* + * Copyright (C) 2008 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "private/android_filesystem_config.h" + +#include <dirent.h> +#include <limits.h> +#include <stdio.h> +#include <stdlib.h> +#include <sys/types.h> +#include <unistd.h> + +/* + * Recursively walk the directory tree at <sysdir>/<subdir>, writing + * script commands to set permissions and create symlinks. + * Assume the contents already have the specified default permissions, + * so only output commands if they need to be changed from the defaults. + * + * Note that permissions are set by fs_config(), which uses a lookup table of + * Android permissions. They are not drawn from the build host filesystem. + */ +static void walk_files( + const char *sysdir, const char *subdir, + unsigned default_uid, unsigned default_gid, + unsigned default_dir_mode, unsigned default_file_mode) { + const char *sep = strcmp(subdir, "") ? "/" : ""; + + char fn[PATH_MAX]; + unsigned dir_uid = 0, dir_gid = 0, dir_mode = 0; + snprintf(fn, PATH_MAX, "system%s%s", sep, subdir); + fs_config(fn, 1, &dir_uid, &dir_gid, &dir_mode); + + snprintf(fn, PATH_MAX, "%s%s%s", sysdir, sep, subdir); + DIR *dir = opendir(fn); + if (dir == NULL) { + perror(fn); + exit(1); + } + + /* + * We can use "set_perm" and "set_perm_recursive" to set file permissions + * (owner, group, and file mode) for individual files and entire subtrees. + * We want to use set_perm_recursive efficiently to avoid setting the + * permissions of every single file in the system image individually. + * + * What we do is recursively set our entire subtree to the permissions + * used by the first file we encounter, and then use "set_perm" to adjust + * the permissions of subsequent files which don't match the first one. + * This is bad if the first file is an outlier, but it generally works. + * Subdirectories can do the same thing recursively if they're different. + */ + + int is_first = 1; + const struct dirent *e; + while ((e = readdir(dir))) { + // Skip over "." and ".." entries + if (!strcmp(e->d_name, ".") || !strcmp(e->d_name, "..")) continue; + + if (e->d_type == DT_LNK) { // Symlink + + // Symlinks don't really have permissions, so this is orthogonal. + snprintf(fn, PATH_MAX, "%s/%s%s%s", sysdir, subdir, sep, e->d_name); + int len = readlink(fn, fn, PATH_MAX - 1); + if (len <= 0) { + perror(fn); + exit(1); + } + fn[len] = '\0'; + printf("symlink %s SYSTEM:%s%s%s\n", fn, subdir, sep, e->d_name); + + } else if (e->d_type == DT_DIR) { // Subdirectory + + // Use the parent directory as the model for default permissions. + // We haven't seen a file, so just make up some file defaults. + if (is_first && ( + dir_mode != default_dir_mode || + dir_uid != default_uid || dir_gid != default_gid)) { + default_uid = dir_uid; + default_gid = dir_gid; + default_dir_mode = dir_mode; + default_file_mode = dir_mode & default_file_mode & 0666; + printf("set_perm_recursive %d %d 0%o 0%o SYSTEM:%s\n", + default_uid, default_gid, + default_dir_mode, default_file_mode, + subdir); + } + + is_first = 0; + + // Recursively handle the subdirectory. + // Note, the recursive call handles the directory's own permissions. + snprintf(fn, PATH_MAX, "%s%s%s", subdir, sep, e->d_name); + walk_files(sysdir, fn, + default_uid, default_gid, + default_dir_mode, default_file_mode); + + } else { // Ordinary file + + // Get the file's desired permissions. + unsigned file_uid = 0, file_gid = 0, file_mode = 0; + snprintf(fn, PATH_MAX, "system/%s%s%s", subdir, sep, e->d_name); + fs_config(fn, 0, &file_uid, &file_gid, &file_mode); + + // If this is the first file, its mode gets to become the default. + if (is_first && ( + dir_mode != default_dir_mode || + file_mode != default_file_mode || + dir_uid != default_uid || file_uid != default_uid || + dir_gid != default_gid || file_gid != default_gid)) { + default_uid = dir_uid; + default_gid = dir_gid; + default_dir_mode = dir_mode; + default_file_mode = file_mode; + printf("set_perm_recursive %d %d 0%o 0%o SYSTEM:%s\n", + default_uid, default_gid, + default_dir_mode, default_file_mode, + subdir); + } + + is_first = 0; + + // Otherwise, override this file if it doesn't match the defaults. + if (file_mode != default_file_mode || + file_uid != default_uid || file_gid != default_gid) { + printf("set_perm %d %d 0%o SYSTEM:%s%s%s\n", + file_uid, file_gid, file_mode, + subdir, sep, e->d_name); + } + + } + } + + // Set the directory's permissions directly, if they never got set. + if (dir_mode != default_dir_mode || + dir_uid != default_uid || dir_gid != default_gid) { + printf("set_perm %d %d 0%o SYSTEM:%s\n", + dir_uid, dir_gid, dir_mode, subdir); + } + + closedir(dir); +} + +/* + * Generate the update script (in "Amend", see commands/recovery/commands.c) + * for the complete-reinstall OTA update packages the build system makes. + * + * The generated script makes a variety of sanity checks about the device, + * erases and reinstalls system files, and sets file permissions appropriately. + */ +int main(int argc, char *argv[]) { + if (argc != 3) { + fprintf(stderr, "usage: %s systemdir android-info.txt >update-script\n", + argv[0]); + return 2; + } + + // ensure basic recovery script language compatibility + printf("assert compatible_with(\"0.2\") == \"true\"\n"); + + // if known, make sure the device name is correct + const char *device = getenv("TARGET_DEVICE"); + if (device != NULL) { + printf("assert getprop(\"ro.product.device\") == \"%s\" || " + "getprop(\"ro.build.product\") == \"%s\"\n", device, device); + } + + // scan android-info.txt to enforce compatibility with the target system + FILE *fp = fopen(argv[2], "r"); + if (fp == NULL) { + perror(argv[2]); + return 1; + } + + // The lines we're looking for look like: + // version-bootloader=x.yy.zzzz + // or: + // require version-bootloader=x.yy.zzzz + char line[256]; + while (fgets(line, sizeof(line), fp)) { + const char *name = strtok(line, "="), *value = strtok(NULL, "\n"); + if (value != NULL && + (!strcmp(name, "version-bootloader") || + !strcmp(name, "require version-bootloader"))) { + printf("assert getprop(\"ro.bootloader\") == \"%s\"\n", value); + } + // We also used to check version-baseband, but we update radio.img + // ourselves, so there's no need. + } + + // erase the boot sector first, so if the update gets interrupted, + // the system will reboot into the recovery partition and start over. + printf("format BOOT:\n"); + + // write the radio image (actually just loads it into RAM for now) + printf("show_progress 0.1 0\n"); + printf("write_radio_image PACKAGE:radio.img\n"); + + // erase and reinstall the system image + printf("show_progress 0.5 0\n"); + printf("format SYSTEM:\n"); + printf("copy_dir PACKAGE:system SYSTEM:\n"); + + // walk the files in the system image, set their permissions, etc. + // use -1 for default values to force permissions to be set explicitly. + walk_files(argv[1], "", -1, -1, -1, -1); + + // as the last step, write the boot sector. + printf("show_progress 0.2 0\n"); + printf("write_raw_image PACKAGE:boot.img BOOT:\n"); + + // after the end of the script, the radio will be written to cache + // leave some space in the progress bar for this operation + printf("show_progress 0.2 10\n"); + return 0; +} |