213 lines
6.9 KiB
C++
213 lines
6.9 KiB
C++
// CUSTOM (TeTeHacko): FwPatch (in-place delta update) against a fake flash.
|
|
// The patches come from tools/ota_fixtures.py; the images are rebuilt here with
|
|
// the same generator, so the header only carries a few kB of patch bytes.
|
|
|
|
#include "fixtures.h"
|
|
#include "helpers/FwPatch.h"
|
|
|
|
#include <cstdlib>
|
|
#include <cstring>
|
|
#include <gtest/gtest.h>
|
|
#include <vector>
|
|
|
|
namespace {
|
|
|
|
std::vector<uint8_t> xorshift(uint32_t x, size_t n) {
|
|
std::vector<uint8_t> out(n);
|
|
for (size_t i = 0; i < n; i++) {
|
|
x ^= x << 13;
|
|
x ^= x >> 17;
|
|
x ^= x << 5;
|
|
out[i] = (uint8_t)(x & 0xFF);
|
|
}
|
|
return out;
|
|
}
|
|
|
|
void make_images(const char *c, std::vector<uint8_t> &old_img, std::vector<uint8_t> &new_img) {
|
|
old_img = xorshift(0x12345678u, 60000);
|
|
if (!strcmp(c, "grow")) {
|
|
auto ins = xorshift(0xCAFEBABEu, 8192);
|
|
new_img.assign(old_img.begin(), old_img.begin() + 5000);
|
|
new_img.insert(new_img.end(), ins.begin(), ins.end());
|
|
new_img.insert(new_img.end(), old_img.begin() + 5000, old_img.end());
|
|
} else if (!strcmp(c, "shrink")) {
|
|
new_img.assign(old_img.begin(), old_img.begin() + 20000);
|
|
new_img.insert(new_img.end(), old_img.begin() + 23000, old_img.end());
|
|
for (size_t i = 0; i < new_img.size(); i += 4093)
|
|
new_img[i] ^= 0x5A;
|
|
} else {
|
|
new_img = old_img;
|
|
for (int i = 0; i < 16; i++)
|
|
new_img[1000 + i * 3001] ^= 0xFF;
|
|
}
|
|
}
|
|
|
|
// Fake flash: page-aligned, erase = 0xFF, write = AND (what NOR flash does).
|
|
struct Flash {
|
|
uint8_t *mem;
|
|
uint32_t size;
|
|
Flash(uint32_t n) : size(n) {
|
|
mem = (uint8_t *)aligned_alloc(FWP_PAGE, n);
|
|
memset(mem, 0xFF, n);
|
|
}
|
|
~Flash() { free(mem); }
|
|
};
|
|
|
|
std::vector<uint8_t> g_sink;
|
|
int g_ticks;
|
|
void sink(void *, const uint8_t *p, uint32_t n) {
|
|
g_sink.insert(g_sink.end(), p, p + n);
|
|
}
|
|
void erase_page(uintptr_t a) {
|
|
memset((void *)a, 0xFF, FWP_PAGE);
|
|
}
|
|
void write_page(uintptr_t a, const uint8_t *s, uint32_t n) {
|
|
uint8_t *d = (uint8_t *)a;
|
|
for (uint32_t i = 0; i < n; i++)
|
|
d[i] &= s[i];
|
|
}
|
|
void tick() {
|
|
g_ticks++;
|
|
}
|
|
|
|
fwp_job job(Flash &f, uint32_t from, const uint8_t *p, uint32_t pn, bool dry) {
|
|
fwp_job j;
|
|
memset(&j, 0, sizeof(j));
|
|
j.mem_base = (uintptr_t)f.mem;
|
|
j.mem_size = f.size;
|
|
j.from_size = from;
|
|
j.patch = p;
|
|
j.patch_size = pn;
|
|
j.dry = dry;
|
|
j.sink = sink;
|
|
j.erase = erase_page;
|
|
j.write = write_page;
|
|
j.tick = tick;
|
|
return j;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
struct Case {
|
|
const char *name;
|
|
uint32_t mem;
|
|
const uint8_t *patch;
|
|
uint32_t patch_size;
|
|
};
|
|
|
|
static const Case kCases[] = {
|
|
{ "grow", fx_grow_mem, fx_grow_patch, sizeof(fx_grow_patch) },
|
|
{ "shrink", fx_shrink_mem, fx_shrink_patch, sizeof(fx_shrink_patch) },
|
|
{ "same", fx_same_mem, fx_same_patch, sizeof(fx_same_patch) },
|
|
};
|
|
|
|
|
|
class FwPatchCase : public ::testing::TestWithParam<Case> {};
|
|
|
|
// The dry run predicts the image exactly and leaves the flash alone; the real
|
|
// run then writes exactly that image.
|
|
TEST_P(FwPatchCase, DryPredictsRealAndFlashUntouched) {
|
|
const Case &c = GetParam();
|
|
std::vector<uint8_t> o, n;
|
|
make_images(c.name, o, n);
|
|
Flash f(c.mem);
|
|
memcpy(f.mem, o.data(), o.size());
|
|
// stale bytes after the image, as a real flash has after a bigger old image
|
|
for (uint32_t i = o.size(); i < o.size() + 3000 && i < f.size; i++)
|
|
f.mem[i] = (uint8_t)(i * 7);
|
|
std::vector<uint8_t> before(f.mem, f.mem + f.size);
|
|
|
|
g_sink.clear();
|
|
fwp_job d = job(f, o.size(), c.patch, c.patch_size, true);
|
|
ASSERT_EQ(fwp_run(&d), 0) << "detools " << d.detools_err;
|
|
EXPECT_EQ(d.to_size, n.size());
|
|
EXPECT_TRUE(g_sink == n);
|
|
EXPECT_EQ(memcmp(before.data(), f.mem, f.size), 0) << "dry run touched the flash";
|
|
|
|
g_ticks = 0;
|
|
fwp_job r = job(f, o.size(), c.patch, c.patch_size, false);
|
|
ASSERT_EQ(fwp_run(&r), 0) << "detools " << r.detools_err;
|
|
EXPECT_EQ(memcmp(f.mem, n.data(), n.size()), 0);
|
|
EXPECT_GT(g_ticks, 0);
|
|
}
|
|
|
|
INSTANTIATE_TEST_SUITE_P(Fixtures, FwPatchCase, ::testing::ValuesIn(kCases),
|
|
[](const ::testing::TestParamInfo<Case> &i) { return std::string(i.param.name); });
|
|
|
|
// The overlap case must really exercise the overlap, otherwise the test above
|
|
// proves less than it claims.
|
|
TEST(FwPatch, GrowCaseOverlapsShiftedAndNewImage) {
|
|
uint32_t mem, seg, shift, from, to;
|
|
ASSERT_EQ(fwp_header(fx_grow_patch, sizeof(fx_grow_patch), &mem, &seg, &shift, &from, &to), 0);
|
|
EXPECT_EQ(seg, FWP_PAGE);
|
|
EXPECT_LT(shift + 0u, to) << "shifted old image and new image do not overlap";
|
|
EXPECT_LE(shift + from, mem);
|
|
}
|
|
|
|
// A wrong base still runs (detools cannot know), but the result differs -- the
|
|
// signed hash in FwUpdate is what rejects it. The dry run must still not touch
|
|
// the flash, and must predict the real run.
|
|
TEST(FwPatch, WrongBaseGivesDifferentImageDryStillPredictsReal) {
|
|
std::vector<uint8_t> o, n;
|
|
make_images("same", o, n);
|
|
o[30000] ^= 0x01;
|
|
Flash f(fx_same_mem);
|
|
memcpy(f.mem, o.data(), o.size());
|
|
g_sink.clear();
|
|
fwp_job d = job(f, o.size(), fx_same_patch, sizeof(fx_same_patch), true);
|
|
ASSERT_EQ(fwp_run(&d), 0);
|
|
EXPECT_FALSE(g_sink == n);
|
|
std::vector<uint8_t> predicted = g_sink;
|
|
fwp_job r = job(f, o.size(), fx_same_patch, sizeof(fx_same_patch), false);
|
|
ASSERT_EQ(fwp_run(&r), 0);
|
|
EXPECT_EQ(memcmp(f.mem, predicted.data(), predicted.size()), 0);
|
|
}
|
|
|
|
TEST(FwPatch, SizeMismatchRejectedBeforeAnything) {
|
|
Flash f(fx_same_mem);
|
|
g_sink.clear();
|
|
fwp_job d = job(f, fx_same_old + 4, fx_same_patch, sizeof(fx_same_patch), true);
|
|
EXPECT_EQ(fwp_run(&d), FWP_ERR_SIZE);
|
|
fwp_job m = job(f, fx_same_old, fx_same_patch, sizeof(fx_same_patch), true);
|
|
m.mem_size += FWP_PAGE;
|
|
EXPECT_EQ(fwp_run(&m), FWP_ERR_SIZE);
|
|
}
|
|
|
|
// Corrupted patches: whatever detools makes of them, the dry run never touches
|
|
// the flash and, when it succeeds, the real run writes exactly its prediction.
|
|
TEST(FwPatch, CorruptedPatchesDryAlwaysPredictsReal) {
|
|
std::vector<uint8_t> o, n;
|
|
make_images("grow", o, n);
|
|
std::vector<uint8_t> p(fx_grow_patch, fx_grow_patch + sizeof(fx_grow_patch));
|
|
uint32_t x = 1;
|
|
int accepted = 0;
|
|
for (int it = 0; it < 200; it++) {
|
|
std::vector<uint8_t> q = p;
|
|
for (int k = 0; k < 2; k++) {
|
|
x ^= x << 13;
|
|
x ^= x >> 17;
|
|
x ^= x << 5;
|
|
q[16 + x % (q.size() - 16)] ^= (uint8_t)(1 + (x >> 8) % 255); // keep the header
|
|
}
|
|
Flash f(fx_grow_mem);
|
|
memcpy(f.mem, o.data(), o.size());
|
|
std::vector<uint8_t> before(f.mem, f.mem + f.size);
|
|
g_sink.clear();
|
|
fwp_job d = job(f, o.size(), q.data(), q.size(), true);
|
|
int rd = fwp_run(&d);
|
|
ASSERT_EQ(memcmp(before.data(), f.mem, f.size), 0) << "dry run touched the flash, it " << it;
|
|
if (rd != 0) continue;
|
|
accepted++;
|
|
std::vector<uint8_t> predicted = g_sink;
|
|
fwp_job r = job(f, o.size(), q.data(), q.size(), false);
|
|
ASSERT_EQ(fwp_run(&r), 0) << "it " << it;
|
|
ASSERT_EQ(memcmp(f.mem, predicted.data(), predicted.size()), 0) << "it " << it;
|
|
}
|
|
RecordProperty("accepted", accepted);
|
|
}
|
|
|
|
int main(int argc, char **argv) {
|
|
::testing::InitGoogleTest(&argc, argv);
|
|
return RUN_ALL_TESTS();
|
|
}
|