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