示例#1
0
gchar *
read_padded_string (const guint8 *buffer, gsize buffer_length, gsize string_length, gsize *offset)
{
    guint8 *value = read_string8 (buffer, buffer_length, string_length, offset);
    read_padding (pad (string_length), offset);
    return (gchar *) value;
}
示例#2
0
文件: sff.c 项目: pawelsm/sff_demult
void
read_sff_common_header(FILE *fp, sff_common_header *h) {
    char *flow;
    char *key;
    int header_size;
    fread(&(h->magic)          , sizeof(uint32_t), 1, fp);
    fread(&(h->version)        , sizeof(char)    , 4, fp);
    fread(&(h->index_offset)   , sizeof(uint64_t), 1, fp);
    fread(&(h->index_len)      , sizeof(uint32_t), 1, fp);
    fread(&(h->nreads)         , sizeof(uint32_t), 1, fp);
    fread(&(h->header_len)     , sizeof(uint16_t), 1, fp);
    fread(&(h->key_len)        , sizeof(uint16_t), 1, fp);
    fread(&(h->flow_len)       , sizeof(uint16_t), 1, fp);
    fread(&(h->flowgram_format), sizeof(uint8_t) , 1, fp);

    /* sff files are in big endian notation so adjust appropriately */
    h->magic        = htobe32(h->magic);
    h->index_offset = htobe64(h->index_offset);
    h->index_len    = htobe32(h->index_len);
    h->nreads       = htobe32(h->nreads);
    h->header_len   = htobe16(h->header_len);
    h->key_len      = htobe16(h->key_len);
    h->flow_len     = htobe16(h->flow_len);

    /* finally appropriately allocate and read the flow and key strings */
    flow = (char *) malloc( h->flow_len * sizeof(char) );
    if (!flow) {
        fprintf(stderr,
                "Out of memory! Could not allocate header flow string!\n");
        exit(1);
    }
    key  = (char *) malloc( h->key_len * sizeof(char) );
    if (!key) {
        fprintf(stderr,
                "Out of memory! Could not allocate header key string!\n");
        exit(1);
    }
    fread(flow, sizeof(char), h->flow_len, fp);
    h->flow = flow;
    fread(key , sizeof(char), h->key_len , fp);
    h->key = key;

    /* the common header section should be a multiple of 8-bytes 
       if the header is not, it is zero-byte padded to make it so */
    header_size = sizeof(h->magic)
                  + sizeof(*(h->version))*4
                  + sizeof(h->index_offset)
                  + sizeof(h->index_len)
                  + sizeof(h->nreads)
                  + sizeof(h->header_len)
                  + sizeof(h->key_len)
                  + sizeof(h->flow_len)
                  + sizeof(h->flowgram_format)
                  + (sizeof(char) * h->flow_len)
                  + (sizeof(char) * h->key_len);
    if ( !(header_size % PADDING_SIZE == 0) ) {
        read_padding(fp, header_size);
    }
}
示例#3
0
文件: sff.c 项目: pawelsm/sff_demult
void
read_sff_read_header(FILE *fp, sff_read_header *rh) {
    char *name;
    int header_size;
    fread(&(rh->header_len)        , sizeof(uint16_t), 1, fp);
    fread(&(rh->name_len)          , sizeof(uint16_t), 1, fp);
    fread(&(rh->nbases)            , sizeof(uint32_t), 1, fp);
    fread(&(rh->clip_qual_left)    , sizeof(uint16_t), 1, fp);
    fread(&(rh->clip_qual_right)   , sizeof(uint16_t), 1, fp);
    fread(&(rh->clip_adapter_left) , sizeof(uint16_t), 1, fp);
    fread(&(rh->clip_adapter_right), sizeof(uint16_t), 1, fp);

    /* sff files are in big endian notation so adjust appropriately */
    rh->header_len         = htobe16(rh->header_len);
    rh->name_len           = htobe16(rh->name_len);
    rh->nbases             = htobe32(rh->nbases);
    rh->clip_qual_left     = htobe16(rh->clip_qual_left);
    rh->clip_qual_right    = htobe16(rh->clip_qual_right);
    rh->clip_adapter_left  = htobe16(rh->clip_adapter_left);
    rh->clip_adapter_right = htobe16(rh->clip_adapter_right);

    /* finally appropriately allocate and read the read_name string */
    name = (char *) malloc( rh->name_len * sizeof(char) );
    if (!name) {
        fprintf(stderr,
                "Out of memory! Could not allocate header flow string!\n");
        exit(1);
    }
    fread(name, sizeof(char), rh->name_len, fp);
    rh->name = name;

    /* the section should be a multiple of 8-bytes, if not,
       it is zero-byte padded to make it so */
    header_size = sizeof(rh->header_len)
                  + sizeof(rh->name_len)
                  + sizeof(rh->nbases)
                  + sizeof(rh->clip_qual_left)
                  + sizeof(rh->clip_qual_right)
                  + sizeof(rh->clip_adapter_left)
                  + sizeof(rh->clip_adapter_right)
                  + (sizeof(char) * rh->name_len);
    if ( !(header_size % PADDING_SIZE == 0) ) {
        read_padding(fp, header_size);
    }
}
示例#4
0
int main(int argc, char** argv)
{
    char base[PATH_MAX];
    char buf[4096];
    char* directory = "./";
    char* filename = NULL;
    int pagesize = 0;
    int curarg = 1;
    int posarg = 0;

    while(curarg < argc) {
        char *arg = argv[curarg++];

        if(!strcmp(arg, "--input") || !strcmp(arg, "-i")) {
            if(curarg >= argc)
                return usage();
            filename = argv[curarg++];
            posarg = 1;
        } else if(!strcmp(arg, "--output") || !strcmp(arg, "-o")) {
            if(curarg >= argc)
                return usage();
            directory = argv[curarg++];
            posarg = 2;
        } else if(!strcmp(arg, "--pagesize") || !strcmp(arg, "-p")) {
            if(curarg >= argc)
                return usage();
            pagesize = strtoul(argv[curarg++], 0, 16);
        } else if(posarg == 0) {
            filename = arg;
            posarg++;
        } else if(posarg == 1) {
            directory = arg;
            posarg++;
        } else {
            return usage();
        }
    }

    if (filename == NULL) {
        return usage();
    }

    mkdir(directory, 0777);

    FILE* f = fopen(filename, "rb");
    boot_img_hdr header;

    int i;
    for (i = 0; i <= 512; i++) {
        fseek(f, i, SEEK_SET);
        fread(buf, BOOT_MAGIC_SIZE, 1, f);
        if (memcmp(buf, BOOT_MAGIC, BOOT_MAGIC_SIZE) == 0)
            break;
    }
    if (i > 512) {
        printf("Android boot magic not found.\n");
        return 1;
    }
    fseek(f, i, SEEK_SET);
    printf("Android magic found at: %d\n", i);

    /* Check for Loki-patched image (https://github.com/djrbliss/loki/) */
    int is_loki = 0;
    fseek(f, i+1024, SEEK_SET);
    fread(buf, 4, 1, f);
    if(memcmp(buf, "LOKI", 4) == 0) {
        printf("Loki-ized image detected.\n");
        is_loki = 1;
    }
    fseek(f, i, SEEK_SET);

    fread(&header, sizeof(header), 1, f);
    /* Fix up Loki-patched image if needed */
    if(is_loki) {
        header.kernel_size = header.dt_size;
        header.dt_size = 0;
        header.ramdisk_size = header.unused;
        header.unused = 0;
        header.ramdisk_addr = header.second_addr;
        header.second_addr = 0;
    }

    printf("BOARD_KERNEL_CMDLINE %s\n", header.cmdline);
    printf("BOARD_KERNEL_BASE %08x\n", header.kernel_addr - 0x00008000);
    printf("BOARD_PAGE_SIZE %d\n", header.page_size);

    if (pagesize == 0) {
        pagesize = header.page_size;
    }

    unsigned baseaddr = header.kernel_addr - 0x00008000;

    sprintf(base, "%s/%s", directory, basename(filename));

    write_string_to_file(base, "cmdline", (char *)header.cmdline);

    sprintf(buf, "%08x", baseaddr);
    write_string_to_file(base, "base", buf);

    sprintf(buf, "--kernel_offset %08x --ramdisk_offset %08x --second_offset %08x --tags_offset %08x",
            header.kernel_addr - baseaddr, header.ramdisk_addr - baseaddr, header.second_addr - baseaddr, header.tags_addr - baseaddr);
    write_string_to_file(base, "offsets", buf);

    sprintf(buf, "%d", header.page_size);
    write_string_to_file(base, "pagesize", buf);

    read_padding(f, sizeof(header), pagesize);


    write_chunk_to_file(f, base, "zImage", header.kernel_size);
    read_padding(f, header.kernel_size, pagesize);

    write_chunk_to_file(f, base, "ramdisk.gz", header.ramdisk_size);
    read_padding(f, header.ramdisk_size, pagesize);

    if(header.second_size) {
        write_chunk_to_file(f, base, "second", header.second_size);
        read_padding(f, header.second_size, pagesize);
    }

    if(header.dt_size) {
        write_chunk_to_file(f, base, "dt", header.dt_size);
        read_padding(f, header.dt_size, pagesize);
    }

    fclose(f);

    return 0;
}
示例#5
0
int main(int argc, char** argv)
{
    char tmp[PATH_MAX];
    char* directory = "./";
    char* filename = NULL;
    int pagesize = 0;

    argc--;
    argv++;
   
    while(argc > 0){
        char *arg = argv[0];
        char *val = argv[1];
        argc -= 2;
        argv += 2;
        if(!strcmp(arg, "--input") || !strcmp(arg, "-i")) {
            filename = val;
        } else if(!strcmp(arg, "--output") || !strcmp(arg, "-o")) {
            directory = val;
        } else if(!strcmp(arg, "--pagesize") || !strcmp(arg, "-p")) {
            pagesize = strtoul(val, 0, 16);
        } else {
            return usage();
        }
    }
    
    if (filename == NULL) {
        return usage();
    }
    
    int total_read = 0;
    FILE* f = fopen(filename, "rb");
    boot_img_hdr header;
    loki_hdr lheader;

    //printf("Reading header...\n");
    fread(&header, sizeof(header), 1, f);
    fseek(f, 0x400, SEEK_SET);
    fread(&lheader, sizeof(lheader), 1, f);
    printf("BOARD_KERNEL_CMDLINE %s\n", header.cmdline);
    printf("BOARD_KERNEL_BASE %08x\n", header.kernel_addr - 0x00008000);
    printf("BOARD_PAGE_SIZE %d\n", header.page_size);
    
    if (pagesize == 0) {
        pagesize = header.page_size;
    }

    if (lheader.magic =="LOKI") {
        header.kernel_size = lheader.orig_kernel_size;
        header.ramdisk_size = lheader.orig_ramdisk_size;
    }
    
    //printf("cmdline...\n");
    sprintf(tmp, "%s/%s", directory, basename(filename));
    strcat(tmp, "-cmdline");
    write_string_to_file(tmp, header.cmdline);
    
    //printf("base...\n");
    sprintf(tmp, "%s/%s", directory, basename(filename));
    strcat(tmp, "-base");
    char basetmp[200];
    sprintf(basetmp, "%08x", header.kernel_addr - 0x00008000);
    write_string_to_file(tmp, basetmp);

    //printf("pagesize...\n");
    sprintf(tmp, "%s/%s", directory, basename(filename));
    strcat(tmp, "-pagesize");
    char pagesizetmp[200];
    sprintf(pagesizetmp, "%d", header.page_size);
    write_string_to_file(tmp, pagesizetmp);
    
    total_read += sizeof(header);
    //printf("total read: %d\n", total_read);
    total_read += read_padding(f, sizeof(header), pagesize);

    sprintf(tmp, "%s/%s", directory, basename(filename));
    strcat(tmp, "-zImage");
    FILE *k = fopen(tmp, "wb");
    byte* kernel = (byte*)malloc(header.kernel_size);
    //printf("Reading kernel...\n");
    fread(kernel, header.kernel_size, 1, f);
    total_read += header.kernel_size;
    fwrite(kernel, header.kernel_size, 1, k);
    fclose(k);

    //printf("total read: %d\n", header.kernel_size);
    total_read += read_padding(f, header.kernel_size, pagesize);

    sprintf(tmp, "%s/%s", directory, basename(filename));
    strcat(tmp, "-ramdisk.gz");
    FILE *r = fopen(tmp, "wb");

    byte* ramdisk = (byte*)malloc(header.ramdisk_size);
    //printf("Reading ramdisk...\n");
    fread(ramdisk, header.ramdisk_size, 1, f);
    total_read += header.ramdisk_size;
    fwrite(ramdisk, header.ramdisk_size, 1, r);
    fclose(r);
    
    fclose(f);
    
    //printf("Total Read: %d\n", total_read);
    return 0;
}
示例#6
0
int main(int argc, char** argv)
{
    char tmp[PATH_MAX];
    char* directory = "./";
    char* filename = NULL;
    int pagesize = 0;

    argc--;
    argv++;
    while(argc > 0){
        char *arg = argv[0];
        char *val = argv[1];
        argc -= 2;
        argv += 2;
        if(!strcmp(arg, "--input") || !strcmp(arg, "-i")) {
            filename = val;
        } else if(!strcmp(arg, "--output") || !strcmp(arg, "-o")) {
            directory = val;
        } else if(!strcmp(arg, "--pagesize") || !strcmp(arg, "-p")) {
            pagesize = strtoul(val, 0, 16);
        } else {
            return usage();
        }
    }
    
    if (filename == NULL) {
        return usage();
    }
    
    int total_read = 0;
    FILE* f = fopen(filename, "rb");
    boot_img_hdr header;

    //printf("Reading header...\n");
    int i;
    for (i = 0; i <= 512; i++) {
        fseek(f, i, SEEK_SET);
        fread(tmp, BOOT_MAGIC_SIZE, 1, f);
        if (memcmp(tmp, BOOT_MAGIC, BOOT_MAGIC_SIZE) == 0)
            break;
    }
    total_read = i;
    if (i > 512) {
        printf("Android boot magic not found.\n");
        return 1;
    }
    fseek(f, i, SEEK_SET);
    printf("Android magic found at: %d\n", i);

    fread(&header, sizeof(header), 1, f);
    printf("BOARD_KERNEL_CMDLINE %s\n", header.cmdline);
    printf("BOARD_KERNEL_BASE %08x\n", header.kernel_addr - 0x00008000);
    printf("BOARD_RAMDISK_OFFSET %08x\n", header.ramdisk_addr - header.kernel_addr + 0x00008000);
    printf("BOARD_SECOND_OFFSET %08x\n", header.second_addr - header.kernel_addr + 0x00008000);
    printf("BOARD_TAGS_OFFSET %08x\n",header.tags_addr - header.kernel_addr + 0x00008000);
    printf("BOARD_PAGE_SIZE %d\n", header.page_size);
    printf("BOARD_SECOND_SIZE %d\n", header.second_size);
    printf("BOARD_DT_SIZE %d\n", header.dt_size);
    
    if (pagesize == 0) {
        pagesize = header.page_size;
    }
    
    //printf("cmdline...\n");
    sprintf(tmp, "%s/%s", directory, basename(filename));
    strcat(tmp, "-cmdline");
    write_string_to_file(tmp, header.cmdline);
    
    //printf("base...\n");
    sprintf(tmp, "%s/%s", directory, basename(filename));
    strcat(tmp, "-base");
    char basetmp[200];
    sprintf(basetmp, "%08x", header.kernel_addr - 0x00008000);
    write_string_to_file(tmp, basetmp);

    //printf("ramdisk_offset...\n");
    sprintf(tmp, "%s/%s", directory, basename(filename));
    strcat(tmp, "-ramdisk_offset");
    char ramdisktmp[200];
    sprintf(ramdisktmp, "%08x", header.ramdisk_addr - header.kernel_addr + 0x00008000);
    write_string_to_file(tmp, ramdisktmp);

    //printf("second_offset...\n");
    sprintf(tmp, "%s/%s", directory, basename(filename));
    strcat(tmp, "-second_offset");
    char secondtmp[200];
    sprintf(secondtmp, "%08x", header.second_addr - header.kernel_addr + 0x00008000);
    write_string_to_file(tmp, secondtmp);

    //printf("tags_offset...\n");
    sprintf(tmp, "%s/%s", directory, basename(filename));
    strcat(tmp, "-tags_offset");
    char tagstmp[200];
    sprintf(tagstmp, "%08x", header.tags_addr - header.kernel_addr + 0x00008000);
    write_string_to_file(tmp, tagstmp);

    //printf("pagesize...\n");
    sprintf(tmp, "%s/%s", directory, basename(filename));
    strcat(tmp, "-pagesize");
    char pagesizetmp[200];
    sprintf(pagesizetmp, "%d", header.page_size);
    write_string_to_file(tmp, pagesizetmp);
    
    total_read += sizeof(header);
    //printf("total read: %d\n", total_read);
    total_read += read_padding(f, sizeof(header), pagesize);

    sprintf(tmp, "%s/%s", directory, basename(filename));
    strcat(tmp, "-zImage");
    FILE *k = fopen(tmp, "wb");
    byte* kernel = (byte*)malloc(header.kernel_size);
    //printf("Reading kernel...\n");
    fread(kernel, header.kernel_size, 1, f);
    total_read += header.kernel_size;
    fwrite(kernel, header.kernel_size, 1, k);
    fclose(k);

    //printf("total read: %d\n", header.kernel_size);
    total_read += read_padding(f, header.kernel_size, pagesize);


    byte* ramdisk = (byte*)malloc(header.ramdisk_size);
    //printf("Reading ramdisk...\n");
    fread(ramdisk, header.ramdisk_size, 1, f);
    total_read += header.ramdisk_size;
    sprintf(tmp, "%s/%s", directory, basename(filename));
    if(ramdisk[0] == 0x02 && ramdisk[1]== 0x21)
        strcat(tmp, "-ramdisk.lz4");
    else
        strcat(tmp, "-ramdisk.gz");
    FILE *r = fopen(tmp, "wb");
    fwrite(ramdisk, header.ramdisk_size, 1, r);
    fclose(r);

    total_read += read_padding(f, header.ramdisk_size, pagesize);

    sprintf(tmp, "%s/%s", directory, basename(filename));
    strcat(tmp, "-second");
    FILE *s = fopen(tmp, "wb");
    byte* second = (byte*)malloc(header.second_size);
    //printf("Reading second...\n");
    fread(second, header.second_size, 1, f);
    total_read += header.second_size;
    fwrite(second, header.second_size, 1, r);
    fclose(s);

    total_read += read_padding(f, header.second_size, pagesize);

    sprintf(tmp, "%s/%s", directory, basename(filename));
    strcat(tmp, "-dt");
    FILE *d = fopen(tmp, "wb");
    byte* dt = (byte*)malloc(header.dt_size);
    //printf("Reading dt...\n");
    fread(dt, header.dt_size, 1, f);
    total_read += header.dt_size;
    fwrite(dt, header.dt_size, 1, r);
    fclose(d);

    total_read += read_padding(f, header.dt_size, pagesize);

    sprintf(tmp, "%s/%s", directory, basename(filename));
    strcat(tmp, "-signature");
    FILE *fsig = fopen(tmp, "wb");
    byte* bsig = (byte*)malloc(256);
    //printf("Reading signature...\n");
    fread(bsig, 256, 1, f);
    total_read += 256;
    fwrite(bsig, 256, 1, r);
    fclose(fsig);

    fclose(f);
    
    //printf("Total Read: %d\n", total_read);
    return 0;
}
int main(int argc, char** argv)
{
    char tmp[PATH_MAX];
    char* directory = "./";
    char* filename = NULL;
    int pagesize = 0;
    int base = 0;
    
    argc--;
    argv++;
    while(argc > 0){
        char *arg = argv[0];
        char *val = argv[1];
        argc -= 2;
        argv += 2;
        if(!strcmp(arg, "--input") || !strcmp(arg, "-i")) {
            filename = val;
        } else if(!strcmp(arg, "--output") || !strcmp(arg, "-o")) {
            directory = val;
        } else if(!strcmp(arg, "--pagesize") || !strcmp(arg, "-p")) {
            pagesize = strtoul(val, 0, 16);
        } else {
            return usage();
        }
    }
    
    if (filename == NULL) {
        return usage();
    }
    
    int total_read = 0;
    FILE* f = fopen(filename, "rb");
    boot_img_hdr header;
    
    //printf("Reading header...\n");
    int i;
    for (i = 0; i <= 512; i++) {
        fseek(f, i, SEEK_SET);
        fread(tmp, BOOT_MAGIC_SIZE, 1, f);
        if (memcmp(tmp, BOOT_MAGIC, BOOT_MAGIC_SIZE) == 0)
            break;
    }
    total_read = i;
    if (i > 512) {
        printf("Android boot magic not found.\n");
        return 1;
    }
    fseek(f, i, SEEK_SET);
    //printf("Android magic found at: %d\n", i);
    
    fread(&header, sizeof(header), 1, f);
    base = header.kernel_addr - 0x00008000;
    
    printf("  %-15s : %d\n", "page_size", header.page_size);
    printf("  %-15s : 0x%08x\n", "base_addr", base);
    //printf("BOARD_NAME %s\n", header.name);
    printf("  %-15s : 0x%08x\n", "kernel_offset", header.kernel_addr - base);
    printf("  %-15s : %d\n", "kernel_size", header.kernel_size);
    printf("  %-15s : 0x%08x\n", "ramdisk_offset", header.ramdisk_addr - base);
    printf("  %-15s : %d\n", "ramdisk_size", header.ramdisk_size);
    printf("  %-15s : 0x%08x\n", "second_offset", header.second_addr - base);
    printf("  %-15s : %d\n", "second_size", header.second_size);
    printf("  %-15s : 0x%08x\n", "tags_offset", header.tags_addr - base);
    printf("  %-15s : %d\n", "dt_size", header.dt_size);
    printf("  %-15s : %s\n", "cmdline", header.cmdline);
    
    if (pagesize == 0) {
        pagesize = header.page_size;
    }

    struct stat st = {0};

    if (stat(directory, &st) == -1) {
        mkdir(directory, 0755);
    }
    
    //printf("cmdline...\n");
    sprintf(tmp, "%s/cmdline", directory);
    remove(tmp);
    write_string_to_file(tmp, (char *)header.cmdline);
    
    sprintf(tmp, "%s/image_info", directory);
    remove(tmp);
    
    //printf("pagesize...\n");
    char pagesizetmp[200];
    sprintf(pagesizetmp, "page_size=%d", header.page_size);
    write_string_to_file(tmp, pagesizetmp);
    
    //printf("base...\n");
    char basetmp[200];
    sprintf(basetmp, "base_addr=0x%08x", base);
    write_string_to_file(tmp, basetmp);
    
    //printf("kerneloff...\n");
    char kernelofftmp[200];
    sprintf(kernelofftmp, "kernel_offset=0x%08x", header.kernel_addr - base);
    write_string_to_file(tmp, kernelofftmp);

    //printf("kernelsize...\n");
    char kernelsizetmp[200];
    sprintf(kernelsizetmp, "kernel_size=%d", header.kernel_size);
    write_string_to_file(tmp, kernelsizetmp);
    
    //printf("ramdiskoff...\n");
    char ramdiskofftmp[200];
    sprintf(ramdiskofftmp, "ramdisk_offset=0x%08x", header.ramdisk_addr - base);
    write_string_to_file(tmp, ramdiskofftmp);
    
    //printf("ramdisksize...\n");
    char ramdisksizetmp[200];
    sprintf(ramdisksizetmp, "ramdisk_size=%d", header.ramdisk_size);
    write_string_to_file(tmp, ramdisksizetmp);
    
    if (header.second_size != 0) {
        //printf("secondoff...\n");
        char secondofftmp[200];
        sprintf(secondofftmp, "second_offset=0x%08x", header.second_addr - base);
        write_string_to_file(tmp, secondofftmp);

        //printf("secondsize...\n");
        char secondsizetmp[200];
        sprintf(secondsizetmp, "second_size=%d", header.second_size);
        write_string_to_file(tmp, secondsizetmp);
    }
    
    total_read += sizeof(header);
    //printf("total read: %d\n", total_read);
    total_read += read_padding(f, sizeof(header), pagesize);
    
    sprintf(tmp, "%s/zImage", directory);
    FILE *k = fopen(tmp, "wb");
    byte* kernel = (byte*)malloc(header.kernel_size);
    //printf("Reading kernel...\n");
    fread(kernel, header.kernel_size, 1, f);
    total_read += header.kernel_size;
    fwrite(kernel, header.kernel_size, 1, k);
    fclose(k);
    
    //printf("total read: %d\n", header.kernel_size);
    total_read += read_padding(f, header.kernel_size, pagesize);
    
    sprintf(tmp, "%s/ramdisk.cpio.gz", directory);
    FILE *r = fopen(tmp, "wb");
    byte* ramdisk = (byte*)malloc(header.ramdisk_size);
    //printf("Reading ramdisk...\n");
    fread(ramdisk, header.ramdisk_size, 1, f);
    total_read += header.ramdisk_size;
    fwrite(ramdisk, header.ramdisk_size, 1, r);
    fclose(r);
    
    //printf("total read: %d\n", header.ramdisk_size);
    total_read += read_padding(f, header.ramdisk_size, pagesize);
    
    if (header.second_size != 0) {
        sprintf(tmp, "%s/second.bin", directory);
        FILE *s = fopen(tmp, "wb");
        byte* second = (byte*)malloc(header.second_size);
        //printf("Reading second...\n");
        fread(second, header.second_size, 1, f);
        total_read += header.second_size;
        fwrite(second, header.second_size, 1, s);
        fclose(s);
    }
    
    //printf("total read: %d\n", header.second_size);
    total_read += read_padding(f, header.second_size, pagesize);

    sprintf(tmp, "%s/dt.img", directory);
    FILE *d = fopen(tmp, "wb");
    byte* dt = (byte*)malloc(header.dt_size);
    //printf("Reading dt...\n");
    fread(dt, header.dt_size, 1, f);
    total_read += header.dt_size;
    fwrite(dt, header.dt_size, 1, r);
    fclose(d);

    total_read += read_padding(f, header.dt_size, pagesize);

    sprintf(tmp, "%s/signature", directory);
    FILE *fsig = fopen(tmp, "wb");
    byte* bsig = (byte*)malloc(256);
    //printf("Reading signature...\n");
    fread(bsig, 256, 1, f);
    total_read += 256;
    fwrite(bsig, 256, 1, r);
    fclose(fsig);
        
    fclose(f);
    
    //printf("Total Read: %d\n", total_read);
    return 0;
}
int unpack_bootimg(int argc, char** argv)
{
    struct stat size_tmp;
    int pagesize = 0;
    int check_magic = 0;
    char tmp[PATH_MAX];
    char* directory = "./";
    char* filename = "boot.img";
    unsigned char boot_magic[8];

    argc--;
    argv++;

    if (argc > 0)
    {
        if (!strcmp(argv[0], "--help"))
            return unpack_bootimg_usage();

        filename = argv[0];
    }

    int total_read = 0;
    FILE *f;
    if ((f = fopen(filename, "rb")) == NULL) {
        printf("\"%s\":no such file or directory\n", filename);
        return -1;
    }

    stat(filename, &size_tmp);
    if(2048 > (int)size_tmp.st_size)
        return unpack_bootimg_usage();

    fseek(f,0,SEEK_SET);
    fread(&boot_magic, sizeof(boot_magic), 1, f);
    if (strcmp(boot_magic, BOOT_MAGIC))
    {
        printf("Android Magic not found!\n");
        return -1;
    }

    fseek(f,-8,SEEK_CUR);
    bootimg_hdr header;
    fread(&header, sizeof(header), 1, f);

    cut();
    printf("Kernel  |addr| = 0x%08x\n" , header.kernel_addr  /*- 0x00008000*/);
    printf("Ramdisk |addr| = 0x%08x\n" , header.ramdisk_addr /*- 0x01000000*/);
    printf("Second  |addr| = 0x%08x\n" , header.second_addr  /*- 0x00f00000*/);
    printf("Tags    |addr| = 0x%08x\n" , header.tags_addr    /*- 0x00000100*/);
    printf("Page    |size| = \"%d\"\n" , header.page_size);
    printf("Board   |char| = \"%s\"\n" , header.name);
    printf("Cmdline |char| = \"%s\"\n" , header.cmdline);
    cut();

    pagesize = header.page_size;
    
    char boottmp[600];
    sprintf(boottmp, "Kernel  [addr] = 0x%08x\n"
                     "Ramdisk [addr] = 0x%08x\n"
                     "Second  [addr] = 0x%08x\n"
                     "Tags    [addr] = 0x%08x\n"
                     "Page    [size] = %d\n"
                     "Board   [char] = %s\n"
                     "Cmdline [char] = %s\n",
    header.kernel_addr, header.ramdisk_addr, header.second_addr,
    header.tags_addr, header.page_size, header.name, header.cmdline);
    write_string_to_file("bootinfo.txt", boottmp);

    total_read += sizeof(header);
    total_read += read_padding(f, sizeof(header), pagesize);

    FILE *k = fopen("kernel","wb");
    byte* kernel = (byte*)malloc(header.kernel_size);

    fread(kernel, header.kernel_size, 1, f);
    total_read += header.kernel_size;
    fwrite(kernel, header.kernel_size, 1, k);
    fclose(k);

    total_read += read_padding(f, header.kernel_size, pagesize);

    FILE *r = fopen("ramdisk.gz", "wb");
    byte* ramdisk = (byte*)malloc(header.ramdisk_size);

    fread(ramdisk, header.ramdisk_size, 1, f);
    total_read += header.ramdisk_size;
    fwrite(ramdisk, header.ramdisk_size, 1, r);
    fclose(r);
    
    fclose(f);

    return 0;
}
示例#9
0
文件: sff.c 项目: pawelsm/sff_demult
void
read_sff_read_data(FILE *fp, 
                   sff_read_data *rd, 
                   uint16_t nflows, 
                   uint32_t nbases) {
    uint16_t *flowgram;
    uint8_t *flow_index;
    uint8_t *quality;
    char *bases;
    int data_size;
    int i;

    /* allocate the appropriate arrays */
    flowgram   = (uint16_t *) malloc( nflows * sizeof(uint16_t) );
    if (!flowgram) {
        fprintf(stderr,
                "Out of memory! Could not allocate for a read flowgram!\n");
        exit(1);
    }
    flow_index = (uint8_t *) malloc( nbases * sizeof(uint8_t)  );
    if (!flow_index) {
        fprintf(stderr,
                "Out of memory! Could not allocate for a read flow index!\n");
        exit(1);
    }
    quality = (uint8_t *) malloc( nbases * sizeof(uint8_t)  );
    if (!quality) {
        fprintf(stderr,
                "Out of memory! Could not allocate for a read quality string!\n");
        exit(1);
    }
    bases = (char * ) malloc( nbases * sizeof(char) );
    if (!bases) {
        fprintf(stderr,
                "Out of memory! Could not allocate for a read base string!\n");
        exit(1);
    }

    /* read from the sff file and assign to sff_read_data */
    fread(flowgram, sizeof(uint16_t), (size_t) nflows, fp);

    /* sff files are in big endian notation so adjust appropriately
     * ENDIAN IS NOT ADJUSTED as it is not needed in current contex*/
    //for (i = 0; i < nflows; i++) {
    //    flowgram[i] = htobe16(flowgram[i]);
    //}
    rd->flowgram = flowgram;
    fread(flow_index, sizeof(uint8_t), (size_t) nbases, fp);
    rd->flow_index = flow_index;
    fread(bases, sizeof(char), (size_t) nbases, fp);
    rd->bases = bases;
    fread(quality, sizeof(uint8_t), (size_t) nbases, fp);
    rd->quality = quality;

    /* the section should be a multiple of 8-bytes, if not,
       it is zero-byte padded to make it so */
    data_size = (sizeof(uint16_t) * nflows)    // flowgram size
                + (sizeof(uint8_t) * nbases)   // flow_index size
                + (sizeof(char) * nbases)      // bases size
                + (sizeof(uint8_t) * nbases);  // quality size
                  
    if ( !(data_size % PADDING_SIZE == 0) ) {
        read_padding(fp, data_size);
    }
}