1 /* 2 * Copyright (C) 2011 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 <stdio.h> 18 #include <errno.h> 19 #include <stdarg.h> 20 #include <stdlib.h> 21 #include <string.h> 22 #include <sys/types.h> 23 #include <sys/stat.h> 24 #include <fcntl.h> 25 #include <unistd.h> 26 #include <cryptfs.h> 27 28 #include "edify/expr.h" 29 #include "bootloader.h" 30 31 Value* WriteBootloaderFn(const char* name, State* state, int argc, Expr* argv[]) 32 { 33 int result = -1; 34 Value* img; 35 Value* xloader_loc; 36 Value* sbl_loc; 37 38 if (argc != 3) { 39 return ErrorAbort(state, "%s() expects 3 args, got %d", name, argc); 40 } 41 42 if (ReadValueArgs(state, argv, 3, &img, &xloader_loc, &sbl_loc) < 0) { 43 return NULL; 44 } 45 46 if(img->type != VAL_BLOB || 47 xloader_loc->type != VAL_STRING || 48 sbl_loc->type != VAL_STRING) { 49 FreeValue(img); 50 FreeValue(xloader_loc); 51 FreeValue(sbl_loc); 52 return ErrorAbort(state, "%s(): argument types are incorrect", name); 53 } 54 55 result = update_bootloader(img->data, img->size, 56 xloader_loc->data, sbl_loc->data); 57 FreeValue(img); 58 FreeValue(xloader_loc); 59 FreeValue(sbl_loc); 60 return StringValue(strdup(result == 0 ? "t" : "")); 61 } 62 63 /* 64 * The size of the userdata partition for HSPA Galaxy Nexus devices is incorrect 65 * in the partition as it comes from the factory. Updating the bootloader fixes 66 * the partition table, and makes the size of the userdata partition 1 sector 67 * smaller. However, if the user had encrypted their device with the original 68 * incorrect size of the partition table, the crypto footer has saved that 69 * size, and tries to map that much data when decrypting. However, with the 70 * new partition table, that size is too big to be mapped, and the kernel 71 * throws an error, and the user can't decrypt and boot the device after the 72 * OTA is installed. Oops! 73 * 74 * The fix here is to recognize a crypto footer that has the wrong size, and 75 * update it to the new correct size. This program should be run as part of 76 * the recovery script for HSPA Galaxy Nexus devices. 77 */ 78 79 #define BAD_SIZE 0x01b14fdfULL 80 #define GOOD_SIZE 0x01b14fdeULL 81 82 #define HSPA_PRIME_KEY_PARTITION "/dev/block/platform/omap/omap_hsmmc.0/by-name/metadata" 83 84 Value* FsSizeFixFn(const char* name, State* state, int argc, Expr* argv[]) 85 { 86 struct crypt_mnt_ftr ftr; 87 int fd; 88 89 if (argc != 0) { 90 return ErrorAbort(state, "%s() expects 0 args, got %d", name, argc); 91 } 92 93 if ((fd = open(HSPA_PRIME_KEY_PARTITION, O_RDWR)) == -1) { 94 return ErrorAbort(state, "%s() Cannot open %s\n", name, HSPA_PRIME_KEY_PARTITION); 95 } 96 97 if (read(fd, &ftr, sizeof(ftr)) != sizeof(ftr)) { 98 close(fd); 99 return ErrorAbort(state, "%s() Cannot read crypto footer %s\n", name, HSPA_PRIME_KEY_PARTITION); 100 } 101 102 if ((ftr.magic == CRYPT_MNT_MAGIC) && (ftr.fs_size == BAD_SIZE)) { 103 ftr.fs_size = GOOD_SIZE; 104 if (lseek(fd, 0, SEEK_SET) == 0) { 105 if (write(fd, &ftr, sizeof(ftr)) == sizeof(ftr)) { 106 fsync(fd); /* Make sure it gets to the disk */ 107 fprintf(stderr, "Footer updated\n"); 108 close(fd); 109 return StringValue(strdup("t")); 110 } 111 } 112 close(fd); 113 return ErrorAbort(state, "%s() Cannot seek or write crypto footer %s\n", name, HSPA_PRIME_KEY_PARTITION); 114 } 115 116 /* Nothing to do */ 117 fprintf(stderr, "Footer doesn't need updating\n"); 118 close(fd); 119 return StringValue(strdup("t")); 120 } 121 122 void Register_librecovery_updater_tuna() { 123 fprintf(stderr, "installing samsung updater extensions\n"); 124 125 RegisterFunction("samsung.write_bootloader", WriteBootloaderFn); 126 RegisterFunction("samsung.fs_size_fix", FsSizeFixFn); 127 } 128