1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
|
#include "idmap.h"
#include <androidfw/AssetManager.h>
#include <androidfw/ResourceTypes.h>
#include <utils/String8.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
using namespace android;
namespace {
static const uint32_t IDMAP_MAGIC = 0x504D4449;
static const size_t PATH_LENGTH = 256;
void printe(const char *fmt, ...);
class IdmapBuffer {
private:
const char* buf_;
size_t len_;
size_t pos_;
public:
IdmapBuffer() : buf_((const char *)MAP_FAILED), len_(0), pos_(0) {}
~IdmapBuffer() {
if (buf_ != MAP_FAILED) {
munmap(const_cast<char*>(buf_), len_);
}
}
status_t init(const char *idmap_path) {
struct stat st;
int fd;
if (stat(idmap_path, &st) < 0) {
printe("failed to stat idmap '%s': %s\n", idmap_path, strerror(errno));
return UNKNOWN_ERROR;
}
len_ = st.st_size;
if ((fd = TEMP_FAILURE_RETRY(open(idmap_path, O_RDONLY))) < 0) {
printe("failed to open idmap '%s': %s\n", idmap_path, strerror(errno));
return UNKNOWN_ERROR;
}
if ((buf_ = (const char*)mmap(NULL, len_, PROT_READ, MAP_PRIVATE, fd, 0)) == MAP_FAILED) {
close(fd);
printe("failed to mmap idmap: %s\n", strerror(errno));
return UNKNOWN_ERROR;
}
close(fd);
return NO_ERROR;
}
status_t nextUint32(uint32_t* i) {
if (!buf_) {
printe("failed to read next uint32_t: buffer not initialized\n");
return UNKNOWN_ERROR;
}
if (pos_ + sizeof(uint32_t) > len_) {
printe("failed to read next uint32_t: end of buffer reached at pos=0x%08x\n",
pos_);
return UNKNOWN_ERROR;
}
if ((reinterpret_cast<uintptr_t>(buf_ + pos_) & 0x3) != 0) {
printe("failed to read next uint32_t: not aligned on 4-byte boundary\n");
return UNKNOWN_ERROR;
}
*i = dtohl(*reinterpret_cast<const uint32_t*>(buf_ + pos_));
pos_ += sizeof(uint32_t);
return NO_ERROR;
}
status_t nextUint16(uint16_t* i) {
if (!buf_) {
printe("failed to read next uint16_t: buffer not initialized\n");
return UNKNOWN_ERROR;
}
if (pos_ + sizeof(uint16_t) > len_) {
printe("failed to read next uint16_t: end of buffer reached at pos=0x%08x\n",
pos_);
return UNKNOWN_ERROR;
}
if ((reinterpret_cast<uintptr_t>(buf_ + pos_) & 0x1) != 0) {
printe("failed to read next uint32_t: not aligned on 2-byte boundary\n");
return UNKNOWN_ERROR;
}
*i = dtohs(*reinterpret_cast<const uint16_t*>(buf_ + pos_));
pos_ += sizeof(uint16_t);
return NO_ERROR;
}
status_t nextPath(char *b) {
if (!buf_) {
printe("failed to read next path: buffer not initialized\n");
return UNKNOWN_ERROR;
}
if (pos_ + PATH_LENGTH > len_) {
printe("failed to read next path: end of buffer reached at pos=0x%08x\n", pos_);
return UNKNOWN_ERROR;
}
memcpy(b, buf_ + pos_, PATH_LENGTH);
pos_ += PATH_LENGTH;
return NO_ERROR;
}
};
void printe(const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
fprintf(stderr, "error: ");
vfprintf(stderr, fmt, ap);
va_end(ap);
}
void print_header() {
printf("SECTION ENTRY VALUE COMMENT\n");
}
void print(const char *section, const char *subsection, uint32_t value, const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
printf("%-12s %-12s 0x%08x ", section, subsection, value);
vprintf(fmt, ap);
printf("\n");
va_end(ap);
}
void print_path(const char *section, const char *subsection, const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
printf("%-12s %-12s .......... ", section, subsection);
vprintf(fmt, ap);
printf("\n");
va_end(ap);
}
status_t resource_metadata(const AssetManager& am, uint32_t res_id,
String8 *package, String8 *type, String8 *name) {
const ResTable& rt = am.getResources();
struct ResTable::resource_name data;
if (!rt.getResourceName(res_id, false, &data)) {
printe("failed to get resource name id=0x%08x\n", res_id);
return UNKNOWN_ERROR;
}
if (package != NULL) {
*package = String8(String16(data.package, data.packageLen));
}
if (type != NULL) {
*type = String8(String16(data.type, data.typeLen));
}
if (name != NULL) {
*name = String8(String16(data.name, data.nameLen));
}
return NO_ERROR;
}
status_t parse_idmap_header(IdmapBuffer& buf, AssetManager& am) {
uint32_t i;
char path[PATH_LENGTH];
status_t err = buf.nextUint32(&i);
if (err != NO_ERROR) {
return err;
}
if (i != IDMAP_MAGIC) {
printe("not an idmap file: actual magic constant 0x%08x does not match expected magic "
"constant 0x%08x\n", i, IDMAP_MAGIC);
return UNKNOWN_ERROR;
}
print_header();
print("IDMAP HEADER", "magic", i, "");
err = buf.nextUint32(&i);
if (err != NO_ERROR) {
return err;
}
print("", "version", i, "");
err = buf.nextUint32(&i);
if (err != NO_ERROR) {
return err;
}
print("", "base crc", i, "");
err = buf.nextUint32(&i);
if (err != NO_ERROR) {
return err;
}
print("", "overlay crc", i, "");
err = buf.nextPath(path);
if (err != NO_ERROR) {
// printe done from IdmapBuffer::nextPath
return err;
}
print_path("", "base path", "%s", path);
if (!am.addAssetPath(String8(path), NULL)) {
printe("failed to add '%s' as asset path\n", path);
return UNKNOWN_ERROR;
}
err = buf.nextPath(path);
if (err != NO_ERROR) {
// printe done from IdmapBuffer::nextPath
return err;
}
print_path("", "overlay path", "%s", path);
return NO_ERROR;
}
status_t parse_data(IdmapBuffer& buf, const AssetManager& am) {
const uint32_t packageId = am.getResources().getBasePackageId(0);
uint16_t data16;
status_t err = buf.nextUint16(&data16);
if (err != NO_ERROR) {
return err;
}
print("DATA HEADER", "target pkg", static_cast<uint32_t>(data16), "");
err = buf.nextUint16(&data16);
if (err != NO_ERROR) {
return err;
}
print("", "types count", static_cast<uint32_t>(data16), "");
uint32_t typeCount = static_cast<uint32_t>(data16);
while (typeCount > 0) {
typeCount--;
err = buf.nextUint16(&data16);
if (err != NO_ERROR) {
return err;
}
const uint32_t targetTypeId = static_cast<uint32_t>(data16);
print("DATA BLOCK", "target type", targetTypeId, "");
err = buf.nextUint16(&data16);
if (err != NO_ERROR) {
return err;
}
print("", "overlay type", static_cast<uint32_t>(data16), "");
err = buf.nextUint16(&data16);
if (err != NO_ERROR) {
return err;
}
const uint32_t entryCount = static_cast<uint32_t>(data16);
print("", "entry count", entryCount, "");
err = buf.nextUint16(&data16);
if (err != NO_ERROR) {
return err;
}
const uint32_t entryOffset = static_cast<uint32_t>(data16);
print("", "entry offset", entryOffset, "");
for (uint32_t i = 0; i < entryCount; i++) {
uint32_t data32;
err = buf.nextUint32(&data32);
if (err != NO_ERROR) {
return err;
}
uint32_t resID = (packageId << 24) | (targetTypeId << 16) | (entryOffset + i);
String8 type;
String8 name;
err = resource_metadata(am, resID, NULL, &type, &name);
if (err != NO_ERROR) {
return err;
}
print("", "entry", data32, "%s/%s", type.string(), name.string());
}
}
return NO_ERROR;
}
}
int idmap_inspect(const char *idmap_path) {
IdmapBuffer buf;
if (buf.init(idmap_path) < 0) {
// printe done from IdmapBuffer::init
return EXIT_FAILURE;
}
AssetManager am;
if (parse_idmap_header(buf, am) != NO_ERROR) {
// printe done from parse_idmap_header
return EXIT_FAILURE;
}
if (parse_data(buf, am) != NO_ERROR) {
// printe done from parse_data_header
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
|