Andrew Jeffery | acee683 | 2018-02-21 22:17:08 +1030 | [diff] [blame^] | 1 | /* |
| 2 | * MBox Daemon Test File |
| 3 | * |
| 4 | * Copyright 2018 IBM |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | * |
| 18 | */ |
| 19 | |
Ratan Gupta | 3214b51 | 2017-05-11 08:58:19 +0530 | [diff] [blame] | 20 | #include "config.h" |
| 21 | #include "mboxd_pnor_partition_table.h" |
| 22 | |
| 23 | extern "C" { |
| 24 | #include "test/mbox.h" |
| 25 | #include "test/system.h" |
| 26 | } |
| 27 | |
| 28 | #include <assert.h> |
| 29 | #include <string.h> |
| 30 | |
| 31 | #include <vector> |
| 32 | #include <fstream> |
| 33 | #include <experimental/filesystem> |
| 34 | |
Adriana Kobylak | 2ad2132 | 2017-11-28 14:59:19 -0600 | [diff] [blame] | 35 | // A read window assumes that the toc is located at offset 0, |
| 36 | // so create dummy partition at arbitrary offset 0x100. |
| 37 | constexpr auto line = "partition01=HBB,00000100,0001000,ECC,PRESERVED"; |
Ratan Gupta | 3214b51 | 2017-05-11 08:58:19 +0530 | [diff] [blame] | 38 | constexpr auto partition = "HBB"; |
| 39 | char tmplt[] = "/tmp/create_read_test.XXXXXX"; |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 40 | uint8_t data[8] = {0xaa, 0x55, 0xaa, 0x66, 0x77, 0x88, 0x99, 0xab}; |
Ratan Gupta | 3214b51 | 2017-05-11 08:58:19 +0530 | [diff] [blame] | 41 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 42 | #define BLOCK_SIZE 4096 |
| 43 | #define MEM_SIZE (BLOCK_SIZE * 2) |
| 44 | #define ERASE_SIZE BLOCK_SIZE |
| 45 | #define N_WINDOWS 1 |
Ratan Gupta | 3214b51 | 2017-05-11 08:58:19 +0530 | [diff] [blame] | 46 | #define WINDOW_SIZE BLOCK_SIZE |
| 47 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 48 | static const uint8_t get_info[] = {0x02, 0x00, 0x02, 0x00, 0x00, 0x00, |
| 49 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 50 | 0x00, 0x00, 0x00, 0x00}; |
Adriana Kobylak | 2ad2132 | 2017-11-28 14:59:19 -0600 | [diff] [blame] | 51 | // offset 0x100 and size 6 |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 52 | static const uint8_t create_read_window[] = {0x04, 0x01, 0x01, 0x00, 0x06, 0x00, |
| 53 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 54 | 0x00, 0x00, 0x00, 0x00}; |
Ratan Gupta | 3214b51 | 2017-05-11 08:58:19 +0530 | [diff] [blame] | 55 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 56 | static const uint8_t response[] = {0x04, 0x01, 0xfe, 0xff, 0x01, 0x00, 0x01, |
| 57 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}; |
Ratan Gupta | 3214b51 | 2017-05-11 08:58:19 +0530 | [diff] [blame] | 58 | |
| 59 | namespace fs = std::experimental::filesystem; |
| 60 | |
| 61 | int main() |
| 62 | { |
| 63 | char* tmpdir = mkdtemp(tmplt); |
| 64 | assert(tmpdir != nullptr); |
| 65 | |
| 66 | // create the toc file |
| 67 | fs::path tocFilePath{tmpdir}; |
| 68 | tocFilePath /= PARTITION_TOC_FILE; |
| 69 | std::ofstream tocFile(tocFilePath.c_str()); |
| 70 | tocFile.write(line, strlen(line)); |
| 71 | tocFile.close(); |
| 72 | |
| 73 | // create the partition file |
| 74 | fs::path partitionFilePath{tmpdir}; |
| 75 | partitionFilePath /= partition; |
| 76 | std::ofstream partitionFile(partitionFilePath.c_str()); |
| 77 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 78 | partitionFile.write((char*)data, sizeof(data)); |
Ratan Gupta | 3214b51 | 2017-05-11 08:58:19 +0530 | [diff] [blame] | 79 | partitionFile.close(); |
| 80 | |
| 81 | system_set_reserved_size(MEM_SIZE); |
| 82 | system_set_mtd_sizes(MEM_SIZE, ERASE_SIZE); |
| 83 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 84 | struct mbox_context* ctx = mbox_create_test_context(N_WINDOWS, WINDOW_SIZE); |
| 85 | strcpy(ctx->paths.ro_loc, tmpdir); |
| 86 | strcpy(ctx->paths.rw_loc, tmpdir); |
| 87 | strcpy(ctx->paths.prsv_loc, tmpdir); |
Ratan Gupta | 3214b51 | 2017-05-11 08:58:19 +0530 | [diff] [blame] | 88 | |
| 89 | vpnor_create_partition_table_from_path(ctx, tmpdir); |
| 90 | |
| 91 | int rc = mbox_command_dispatch(ctx, get_info, sizeof(get_info)); |
| 92 | assert(rc == 1); |
| 93 | |
| 94 | // send the request for partition1 |
| 95 | rc = mbox_command_dispatch(ctx, create_read_window, |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 96 | sizeof(create_read_window)); |
Ratan Gupta | 3214b51 | 2017-05-11 08:58:19 +0530 | [diff] [blame] | 97 | assert(rc == 1); |
| 98 | |
| 99 | rc = mbox_cmp(ctx, response, sizeof(response)); |
| 100 | assert(rc == 0); |
| 101 | |
| 102 | // Compare the reserved memory to the pnor |
| 103 | rc = memcmp(ctx->mem, data, 6); |
| 104 | assert(rc == 0); |
| 105 | |
Andrew Jeffery | f34db31 | 2018-03-09 15:27:03 +1030 | [diff] [blame] | 106 | // TODO: Add few more test cases for read from multiple partitions(PRSV/RW) |
Ratan Gupta | 6a98e18 | 2017-06-06 15:04:23 +0530 | [diff] [blame] | 107 | // Read beyond the partition file size. |
| 108 | // openbmc/openbmc#1868 |
| 109 | |
Ratan Gupta | 3214b51 | 2017-05-11 08:58:19 +0530 | [diff] [blame] | 110 | fs::remove_all(fs::path{tmpdir}); |
| 111 | return rc; |
| 112 | } |