예제 #1
0
TEST(BootImgHeaderTest, CheckDefaultValues)
{
    Header header;

    ASSERT_EQ(header.supported_fields(), ALL_FIELDS);

    ASSERT_FALSE(header.board_name());
    ASSERT_FALSE(header.kernel_cmdline());
    ASSERT_FALSE(header.page_size());
    ASSERT_FALSE(header.kernel_address());
    ASSERT_FALSE(header.ramdisk_address());
    ASSERT_FALSE(header.secondboot_address());
    ASSERT_FALSE(header.kernel_tags_address());
    ASSERT_FALSE(header.sony_ipl_address());
    ASSERT_FALSE(header.sony_rpm_address());
    ASSERT_FALSE(header.sony_appsbl_address());
    ASSERT_FALSE(header.entrypoint_address());
}
예제 #2
0
oc::result<void> MtkFormatWriter::write_header(File &file, const Header &header)
{
    // Construct header
    m_hdr = {};
    memcpy(m_hdr.magic, android::BOOT_MAGIC, android::BOOT_MAGIC_SIZE);

    if (auto address = header.kernel_address()) {
        m_hdr.kernel_addr = *address;
    }
    if (auto address = header.ramdisk_address()) {
        m_hdr.ramdisk_addr = *address;
    }
    if (auto address = header.secondboot_address()) {
        m_hdr.second_addr = *address;
    }
    if (auto address = header.kernel_tags_address()) {
        m_hdr.tags_addr = *address;
    }
    if (auto page_size = header.page_size()) {
        switch (*page_size) {
        case 2048:
        case 4096:
        case 8192:
        case 16384:
        case 32768:
        case 65536:
        case 131072:
            m_hdr.page_size = *page_size;
            break;
        default:
            //DEBUG("Invalid page size: %" PRIu32, *page_size);
            return android::AndroidError::InvalidPageSize;
        }
    } else {
        return android::AndroidError::MissingPageSize;
    }

    if (auto board_name = header.board_name()) {
        if (board_name->size() >= sizeof(m_hdr.name)) {
            return android::AndroidError::BoardNameTooLong;
        }

        strncpy(reinterpret_cast<char *>(m_hdr.name), board_name->c_str(),
                sizeof(m_hdr.name) - 1);
        m_hdr.name[sizeof(m_hdr.name) - 1] = '\0';
    }
    if (auto cmdline = header.kernel_cmdline()) {
        if (cmdline->size() >= sizeof(m_hdr.cmdline)) {
            return android::AndroidError::KernelCmdlineTooLong;
        }

        strncpy(reinterpret_cast<char *>(m_hdr.cmdline), cmdline->c_str(),
                sizeof(m_hdr.cmdline) - 1);
        m_hdr.cmdline[sizeof(m_hdr.cmdline) - 1] = '\0';
    }

    // TODO: UNUSED
    // TODO: ID

    std::vector<SegmentWriterEntry> entries;

    entries.push_back({ ENTRY_TYPE_MTK_KERNEL_HEADER, 0, {}, 0 });
    entries.push_back({ ENTRY_TYPE_KERNEL, 0, {}, m_hdr.page_size });
    entries.push_back({ ENTRY_TYPE_MTK_RAMDISK_HEADER, 0, {}, 0 });
    entries.push_back({ ENTRY_TYPE_RAMDISK, 0, {}, m_hdr.page_size });
    entries.push_back({ ENTRY_TYPE_SECONDBOOT, 0, {}, m_hdr.page_size });
    entries.push_back({ ENTRY_TYPE_DEVICE_TREE, 0, {}, m_hdr.page_size });

    OUTCOME_TRYV(m_seg->set_entries(std::move(entries)));

    // Start writing after first page
    auto seek_ret = file.seek(m_hdr.page_size, SEEK_SET);
    if (!seek_ret) {
        if (file.is_fatal()) { m_writer.set_fatal(); }
        return seek_ret.as_failure();
    }

    return oc::success();
}
예제 #3
0
TEST(BootImgHeaderTest, CheckGettersSetters)
{
    Header header;

    // Board name field

    header.set_board_name({"test"});
    auto board_name = header.board_name();
    ASSERT_TRUE(board_name);
    ASSERT_EQ(*board_name, "test");

    header.set_board_name({});
    ASSERT_FALSE(header.board_name());

    // Kernel cmdline field

    header.set_kernel_cmdline({"test"});
    auto kernel_cmdline = header.kernel_cmdline();
    ASSERT_TRUE(kernel_cmdline);
    ASSERT_EQ(*kernel_cmdline, "test");

    header.set_kernel_cmdline({});
    ASSERT_FALSE(header.kernel_cmdline());

    // Page size field

    header.set_page_size(1234);
    auto page_size = header.page_size();
    ASSERT_TRUE(page_size);
    ASSERT_EQ(*page_size, 1234u);

    header.set_page_size({});
    ASSERT_FALSE(header.page_size());

    // Kernel address field

    header.set_kernel_address(1234);
    auto kernel_address = header.kernel_address();
    ASSERT_TRUE(kernel_address);
    ASSERT_EQ(*kernel_address, 1234u);

    header.set_kernel_address({});
    ASSERT_FALSE(header.kernel_address());

    // Ramdisk address field

    header.set_ramdisk_address(1234);
    auto ramdisk_address = header.ramdisk_address();
    ASSERT_TRUE(ramdisk_address);
    ASSERT_EQ(*ramdisk_address, 1234u);

    header.set_ramdisk_address({});
    ASSERT_FALSE(header.ramdisk_address());

    // Second bootloader address field

    header.set_secondboot_address(1234);
    auto secondboot_address = header.secondboot_address();
    ASSERT_TRUE(secondboot_address);
    ASSERT_EQ(*secondboot_address, 1234u);

    header.set_secondboot_address({});
    ASSERT_FALSE(header.secondboot_address());

    // Kernel tags address field

    header.set_kernel_tags_address(1234);
    auto kernel_tags_address = header.kernel_tags_address();
    ASSERT_TRUE(kernel_tags_address);
    ASSERT_EQ(*kernel_tags_address, 1234u);

    header.set_kernel_tags_address({});
    ASSERT_FALSE(header.kernel_tags_address());

    // Sony IPL address field

    header.set_sony_ipl_address(1234);
    auto sony_ipl_address = header.sony_ipl_address();
    ASSERT_TRUE(sony_ipl_address);
    ASSERT_EQ(*sony_ipl_address, 1234u);

    header.set_sony_ipl_address({});
    ASSERT_FALSE(header.sony_ipl_address());

    // Sony RPM address field

    header.set_sony_rpm_address(1234);
    auto sony_rpm_address = header.sony_rpm_address();
    ASSERT_TRUE(sony_rpm_address);
    ASSERT_EQ(*sony_rpm_address, 1234u);

    header.set_sony_rpm_address({});
    ASSERT_FALSE(header.sony_rpm_address());

    // Sony APPSBL address field

    header.set_sony_appsbl_address(1234);
    auto sony_appsbl_address = header.sony_appsbl_address();
    ASSERT_TRUE(sony_appsbl_address);
    ASSERT_EQ(*sony_appsbl_address, 1234u);

    header.set_sony_appsbl_address({});
    ASSERT_FALSE(header.sony_appsbl_address());

    // Entrypoint address field

    header.set_entrypoint_address(1234);
    auto entrypoint_address = header.entrypoint_address();
    ASSERT_TRUE(entrypoint_address);
    ASSERT_EQ(*entrypoint_address, 1234u);

    header.set_entrypoint_address({});
    ASSERT_FALSE(header.entrypoint_address());
}
예제 #4
0
TEST(BootImgHeaderTest, CheckSettingUnsupported)
{
    Header header;

    // Board name field

    header.set_supported_fields(header.supported_fields()
            .set_flag(HeaderField::BoardName, false));

    ASSERT_FALSE(header.set_board_name({"test"}));
    ASSERT_FALSE(header.board_name());
    ASSERT_FALSE(header.set_board_name({}));

    // Kernel cmdline field

    header.set_supported_fields(header.supported_fields()
            .set_flag(HeaderField::KernelCmdline, false));

    ASSERT_FALSE(header.set_kernel_cmdline({"test"}));
    ASSERT_FALSE(header.kernel_cmdline());
    ASSERT_FALSE(header.set_kernel_cmdline({}));

    // Page size field

    header.set_supported_fields(header.supported_fields()
            .set_flag(HeaderField::PageSize, false));

    ASSERT_FALSE(header.set_page_size(1234));
    ASSERT_FALSE(header.page_size());
    ASSERT_FALSE(header.set_page_size({}));

    // Kernel address field

    header.set_supported_fields(header.supported_fields()
            .set_flag(HeaderField::KernelAddress, false));

    ASSERT_FALSE(header.set_kernel_address(1234));
    ASSERT_FALSE(header.kernel_address());
    ASSERT_FALSE(header.set_kernel_address({}));

    // Ramdisk address field

    header.set_supported_fields(header.supported_fields()
            .set_flag(HeaderField::RamdiskAddress, false));

    ASSERT_FALSE(header.set_ramdisk_address(1234));
    ASSERT_FALSE(header.ramdisk_address());
    ASSERT_FALSE(header.set_ramdisk_address({}));

    // Second bootloader address field

    header.set_supported_fields(header.supported_fields()
            .set_flag(HeaderField::SecondbootAddress, false));

    ASSERT_FALSE(header.set_secondboot_address(1234));
    ASSERT_FALSE(header.secondboot_address());
    ASSERT_FALSE(header.set_secondboot_address({}));

    // Kernel tags address field

    header.set_supported_fields(header.supported_fields()
            .set_flag(HeaderField::KernelTagsAddress, false));

    ASSERT_FALSE(header.set_kernel_tags_address(1234));
    ASSERT_FALSE(header.kernel_tags_address());
    ASSERT_FALSE(header.set_kernel_tags_address({}));

    // Sony IPL address field

    header.set_supported_fields(header.supported_fields()
            .set_flag(HeaderField::SonyIplAddress, false));

    ASSERT_FALSE(header.set_sony_ipl_address(1234));
    ASSERT_FALSE(header.sony_ipl_address());
    ASSERT_FALSE(header.set_sony_ipl_address({}));

    // Sony RPM address field

    header.set_supported_fields(header.supported_fields()
            .set_flag(HeaderField::SonyRpmAddress, false));

    ASSERT_FALSE(header.set_sony_rpm_address(1234));
    ASSERT_FALSE(header.sony_rpm_address());
    ASSERT_FALSE(header.set_sony_rpm_address({}));

    // Sony APPSBL address field

    header.set_supported_fields(header.supported_fields()
            .set_flag(HeaderField::SonyAppsblAddress, false));

    ASSERT_FALSE(header.set_sony_appsbl_address(1234));
    ASSERT_FALSE(header.sony_appsbl_address());
    ASSERT_FALSE(header.set_sony_appsbl_address({}));

    // Entrypoint address field

    header.set_supported_fields(header.supported_fields()
            .set_flag(HeaderField::Entrypoint, false));

    ASSERT_FALSE(header.set_entrypoint_address(1234));
    ASSERT_FALSE(header.entrypoint_address());
    ASSERT_FALSE(header.set_entrypoint_address({}));
}