Ratan Gupta | 3214b51 | 2017-05-11 08:58:19 +0530 | [diff] [blame] | 1 | #include "config.h" |
| 2 | #include "mboxd_pnor_partition_table.h" |
| 3 | |
| 4 | extern "C" { |
| 5 | #include "test/mbox.h" |
| 6 | #include "test/system.h" |
| 7 | } |
| 8 | |
| 9 | #include <assert.h> |
| 10 | #include <string.h> |
| 11 | |
| 12 | #include <vector> |
| 13 | #include <fstream> |
| 14 | #include <experimental/filesystem> |
| 15 | |
Adriana Kobylak | 2ad2132 | 2017-11-28 14:59:19 -0600 | [diff] [blame] | 16 | // A read window assumes that the toc is located at offset 0, |
| 17 | // so create dummy partition at arbitrary offset 0x100. |
| 18 | constexpr auto line = "partition01=HBB,00000100,0001000,ECC,PRESERVED"; |
Ratan Gupta | 3214b51 | 2017-05-11 08:58:19 +0530 | [diff] [blame] | 19 | constexpr auto partition = "HBB"; |
| 20 | char tmplt[] = "/tmp/create_read_test.XXXXXX"; |
| 21 | uint8_t data[8] = { 0xaa, 0x55, 0xaa, 0x66, 0x77, 0x88, 0x99, 0xab }; |
| 22 | |
| 23 | #define BLOCK_SIZE 4096 |
| 24 | #define MEM_SIZE (BLOCK_SIZE *2) |
| 25 | #define ERASE_SIZE BLOCK_SIZE |
| 26 | #define N_WINDOWS 1 |
| 27 | #define WINDOW_SIZE BLOCK_SIZE |
| 28 | |
| 29 | static const uint8_t get_info[] = { |
| 30 | 0x02, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 31 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 |
| 32 | }; |
Adriana Kobylak | 2ad2132 | 2017-11-28 14:59:19 -0600 | [diff] [blame] | 33 | // offset 0x100 and size 6 |
Ratan Gupta | 3214b51 | 2017-05-11 08:58:19 +0530 | [diff] [blame] | 34 | static const uint8_t create_read_window[] = { |
| 35 | 0x04, 0x01, 0x01, 0x00, 0x06, 0x00, 0x00, 0x00, |
| 36 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 |
| 37 | }; |
| 38 | |
| 39 | static const uint8_t response[] = { |
| 40 | 0x04, 0x01, 0xfe, 0xff, 0x01, 0x00, 0x01, 0x00, |
| 41 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 |
| 42 | }; |
| 43 | |
| 44 | namespace fs = std::experimental::filesystem; |
| 45 | |
| 46 | int main() |
| 47 | { |
| 48 | char* tmpdir = mkdtemp(tmplt); |
| 49 | assert(tmpdir != nullptr); |
| 50 | |
| 51 | // create the toc file |
| 52 | fs::path tocFilePath{tmpdir}; |
| 53 | tocFilePath /= PARTITION_TOC_FILE; |
| 54 | std::ofstream tocFile(tocFilePath.c_str()); |
| 55 | tocFile.write(line, strlen(line)); |
| 56 | tocFile.close(); |
| 57 | |
| 58 | // create the partition file |
| 59 | fs::path partitionFilePath{tmpdir}; |
| 60 | partitionFilePath /= partition; |
| 61 | std::ofstream partitionFile(partitionFilePath.c_str()); |
| 62 | |
| 63 | partitionFile.write((char*)data,sizeof(data)); |
| 64 | partitionFile.close(); |
| 65 | |
| 66 | system_set_reserved_size(MEM_SIZE); |
| 67 | system_set_mtd_sizes(MEM_SIZE, ERASE_SIZE); |
| 68 | |
| 69 | struct mbox_context *ctx = mbox_create_test_context(N_WINDOWS, WINDOW_SIZE); |
| 70 | strcpy(ctx->paths.ro_loc,tmpdir); |
Ratan Gupta | c0ef987 | 2017-06-06 14:31:37 +0530 | [diff] [blame] | 71 | strcpy(ctx->paths.rw_loc,tmpdir); |
| 72 | strcpy(ctx->paths.prsv_loc,tmpdir); |
Ratan Gupta | 3214b51 | 2017-05-11 08:58:19 +0530 | [diff] [blame] | 73 | |
| 74 | vpnor_create_partition_table_from_path(ctx, tmpdir); |
| 75 | |
| 76 | int rc = mbox_command_dispatch(ctx, get_info, sizeof(get_info)); |
| 77 | assert(rc == 1); |
| 78 | |
| 79 | // send the request for partition1 |
| 80 | rc = mbox_command_dispatch(ctx, create_read_window, |
| 81 | sizeof(create_read_window)); |
| 82 | assert(rc == 1); |
| 83 | |
| 84 | rc = mbox_cmp(ctx, response, sizeof(response)); |
| 85 | assert(rc == 0); |
| 86 | |
| 87 | // Compare the reserved memory to the pnor |
| 88 | rc = memcmp(ctx->mem, data, 6); |
| 89 | assert(rc == 0); |
| 90 | |
Ratan Gupta | 6a98e18 | 2017-06-06 15:04:23 +0530 | [diff] [blame] | 91 | //TODO: Add few more test cases for read from multiple partitions(PRSV/RW) |
| 92 | // Read beyond the partition file size. |
| 93 | // openbmc/openbmc#1868 |
| 94 | |
Ratan Gupta | 3214b51 | 2017-05-11 08:58:19 +0530 | [diff] [blame] | 95 | fs::remove_all(fs::path{tmpdir}); |
| 96 | return rc; |
| 97 | } |