Skip to content

Commit

Permalink
imgpatchtools: cleanup and add ApplyPatch
Browse files Browse the repository at this point in the history
ApplyPatch to patch with .p file like boot.img.p on OTA

Signed-off-by: erfanoabdi <[email protected]>
  • Loading branch information
erfanoabdi committed Jul 22, 2017
1 parent 2597e1c commit 03374f8
Show file tree
Hide file tree
Showing 9 changed files with 1,187 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@

cache
Progress.txt
ApplyPatchfn
115 changes: 115 additions & 0 deletions ApplyPatch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
//
// ApplyPatch.cpp
// imgpatchtools
//
// Created by Erfan Abdi on 7/19/17.
// Copyright © 2017 Erfan Abdi. All rights reserved.
//

#include <iostream>
#include "applypatch/applypatch.h"
#include "edify/expr.h"

#include <memory>
#include <string>
#include <vector>

#include <android-base/parseint.h>
#include <android-base/strings.h>
#include <android-base/stringprintf.h>

// <file> <target> <tgt_sha1> <size> <init_sha1> <patch>
int ApplyPatchFn(const char* name, State* state, int argc, char * argv[]) {
printf("hey\n");
char* source_filename = argv[1];
char* target_filename = argv[2];
char* target_sha1 = argv[3];
char* target_size_str = argv[4];

size_t target_size;
if (!android::base::ParseUint(target_size_str, &target_size)) {
printf("%s(): can't parse \"%s\" as byte count\n",
name, target_size_str);
free(source_filename);
free(target_filename);
free(target_sha1);
free(target_size_str);
return kArgsParsingFailure;
}

int patchcount = (argc-4) / 2;

//std::vector<std::unique_ptr<Value, decltype(&FreeValue)>> patch_shas;
//std::vector<std::unique_ptr<Value, decltype(&FreeValue)>> patches;

std::vector<char*> patch_sha_str;
std::vector<Value*> patch_ptrs;

// Protect values by unique_ptrs first to get rid of memory leak.
for (int i = 0; i < patchcount * 2; i += 2) {
//patch_shas.emplace_back(StringValue(strdup(argv[i])), FreeValue);

printf("i=%i patchcount=%i argc=%i argv[i]=%s argv[i+1]=%s\n",i,patchcount,argc,argv[i+5],argv[i+6]);

Value patch_value;
patch_value.type = VAL_BLOB;

FILE *rm;
int length;
rm = fopen(argv[i+6], "r");
fseek (rm, 0, SEEK_END);
length = ftell (rm);
fseek (rm, 0, SEEK_SET);
patch_value.data = (char*)malloc ((length+1)*sizeof(char));

if (patch_value.data)
{
fread (patch_value.data, sizeof(char), length, rm);
}
fclose (rm);

patch_value.size = length;
//patches.emplace_back(patch_value, FreeValue);

patch_sha_str.push_back(argv[i+5]);
patch_ptrs.push_back(&patch_value);
}

/*for (int i = 0; i < patchcount; ++i) {
if (patch_shas[i]->type != VAL_STRING) {
printf("%s(): sha-1 #%d is not string", name, i);
return kArgsParsingFailure;
}
if (patches[i]->type != VAL_BLOB) {
printf("%s(): patch #%d is not blob", name, i);
return kArgsParsingFailure;
}
}
std::vector<char*> patch_sha_str;
std::vector<Value*> patch_ptrs;
for (int i = 0; i < patchcount; ++i) {
patch_sha_str.push_back(patch_shas[i]->data);
patch_ptrs.push_back(patches[i].get());
}*/
printf("applypatch\n");
int result = applypatch(source_filename, target_filename,
target_sha1, target_size,
patchcount, patch_sha_str.data(), patch_ptrs.data(), NULL);

return result;
}

int main(int argc, char * argv[]) {
if (argc < 7 || (argc % 2) == 0) {
printf("usage: %s <file> <target> <tgt_sha1> <size> <init_sha1(s)> <patch(s)>\n",argv[0]);
return 0;
}

State* state;
int i;
i=ApplyPatchFn("ApplyPatchFn", state, argc, argv);

std::cout << "Done with error code : " << i << "\n";
return 0;
}
4 changes: 2 additions & 2 deletions BlockImageUpdate.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// BlockImageUpdateFn.cpp
// updater
// BlockImageUpdate.cpp
// imgpatchtools
//
// Created by Erfan Abdi on 7/19/17.
// Copyright © 2017 Erfan Abdi. All rights reserved.
Expand Down
4 changes: 2 additions & 2 deletions BlockImageVerify.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// BlockImageVerifyFn.cpp
// updater
// BlockImageVerify.cpp
// imgpatchtools
//
// Created by Erfan Abdi on 7/19/17.
// Copyright © 2017 Erfan Abdi. All rights reserved.
Expand Down
10 changes: 8 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ EXE = .exe
RM = del
endif

CFLAGS = -ffunction-sections -O3 -std=c++11
CFLAGS = -ffunction-sections -O3 -std=c++11
LDFLAGS = -lssl -lcrypto -lz -lbz2 -lpthread
INC = -I. -Iinclude/ -I../include/ -I/usr/local/include/ -I../

Expand All @@ -23,7 +23,7 @@ endif

SUBDIRS = applypatch android-base edify minzip otafault blockimg

all:sub BlockImageVerify.o BlockImageUpdate.o BlockImageVerify$(EXE) BlockImageUpdate$(EXE) imgdiff.o imgdiff$(EXE)
all:sub BlockImageVerify.o BlockImageUpdate.o imgdiff.o ApplyPatch.o BlockImageVerify$(EXE) BlockImageUpdate$(EXE) imgdiff$(EXE) ApplyPatchfn$(EXE)

sub:
for dir in $(SUBDIRS); do \
Expand All @@ -49,6 +49,12 @@ imgdiff.o:imgdiff.cpp
imgdiff$(EXE):imgdiff.o edify/expr.o android-base/stringprintf.o android-base/strings.o minzip/Hash.o applypatch/bspatch.o applypatch/bsdiff.o applypatch/imgpatch.o applypatch/imgdiff.o applypatch/utils.o otafault/ota_io.o
$(CROSS_COMPILE)$(PP) -o $@ $^ $(LDFLAGS) -s

ApplyPatch.o:ApplyPatch.cpp
$(CROSS_COMPILE)$(PP) -o $@ $(CFLAGS) -c $< $(INC)

ApplyPatchfn$(EXE):ApplyPatch.o applypatch/applypatch.o edify/expr.o android-base/stringprintf.o android-base/strings.o minzip/Hash.o applypatch/bspatch.o applypatch/bsdiff.o applypatch/imgpatch.o applypatch/imgdiff.o applypatch/utils.o otafault/ota_io.o
$(CROSS_COMPILE)$(PP) -o $@ $^ $(LDFLAGS) -s

clean:
find -name '*.o' -exec rm {} \;
$(RM) BlockImageVerify BlockImageUpdate imgdiff
6 changes: 3 additions & 3 deletions applypatch/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ else
LDFLAGS +=
endif

all:bspatch.o bsdiff.o imgpatch.o imgdiff.o utils.o
all:bspatch.o bsdiff.o imgpatch.o imgdiff.o utils.o applypatch.o
#imgdiff$(EXE)

bspatch.o:bspatch.cpp
Expand All @@ -42,8 +42,8 @@ utils.o:utils.cpp
freecache.o:freecache.cpp
$(CROSS_COMPILE)$(PP) -o $@ $(CFLAGS) -c $< $(INC)

#imgdiff$(EXE):imgdiff.o ../edify/expr.o ../android-base/stringprintf.o ../android-base/strings.o ../minzip/Hash.o bspatch.o bsdiff.o imgpatch.o imgdiff.o utils.o
$(CROSS_COMPILE)$(PP) -o $@ $^ $(LDFLAGS) -s
applypatch.o:applypatch.cpp
$(CROSS_COMPILE)$(PP) -o $@ $(CFLAGS) -c $< $(INC)

clean:
$(RM) bspatch.o bsdiff.o imgpatch.o imgdiff.o utils.o
Loading

0 comments on commit 03374f8

Please sign in to comment.