1 // SPDX-License-Identifier: Apache-2.0
2 // Copyright (C) 2018 IBM Corp.
3 
4 #include <assert.h>
5 #include <string.h>
6 
7 #include "config.h"
8 #include "transport_mbox.h"
9 #include "windows.h"
10 
11 #include "test/mbox.h"
12 #include "test/system.h"
13 
14 struct test_context
15 {
16     uint8_t seq;
17     struct mbox_context *ctx;
18 };
19 
20 // Configure the system and the paritions such that we eventually request a
21 // window that covers the last section of flash, but the remaining flash is
22 // smaller than the window size
23 #define BLOCK_SIZE	4096
24 #define ERASE_SIZE	BLOCK_SIZE
25 #define N_WINDOWS	3
26 #define WINDOW_SIZE	BLOCK_SIZE
27 #define MEM_SIZE	(N_WINDOWS * WINDOW_SIZE)
28 #define PNOR_SIZE	((N_WINDOWS + 1) * WINDOW_SIZE)
29 
30 static const uint8_t get_info[] = {0x02, 0x00, 0x02, 0x00, 0x00, 0x00,
31                                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
32                                    0x00, 0x00, 0x00, 0x00};
33 
34 #define MBOX_CREATE_READ_WINDOW 4
35 
36 static int mbox_create_read_window(struct test_context *tctx, size_t offset,
37                                    size_t len)
38 {
39     union mbox_regs regs;
40 
41     memset(&regs, 0, sizeof(regs));
42     regs.msg.command = MBOX_CREATE_READ_WINDOW;
43     regs.msg.seq = ++tctx->seq;
44     put_u16(&regs.msg.args[0], offset);
45     put_u16(&regs.msg.args[2], len);
46 
47     return mbox_command_dispatch(tctx->ctx, regs.raw, sizeof(regs.raw));
48 }
49 
50 int main()
51 {
52     struct test_context _tctx = {0}, *tctx = &_tctx;
53     size_t len;
54     size_t pos;
55     int rc;
56 
57     system_set_reserved_size(MEM_SIZE);
58     system_set_mtd_sizes(PNOR_SIZE, ERASE_SIZE);
59 
60     tctx->ctx = mbox_create_test_context(N_WINDOWS, WINDOW_SIZE);
61 
62     rc = mbox_command_dispatch(tctx->ctx, get_info, sizeof(get_info));
63     assert(rc == 1);
64 
65     pos = 0;
66     while (pos < ((PNOR_SIZE - BLOCK_SIZE) / BLOCK_SIZE))
67     {
68         struct mbox_msg _msg, *msg = &_msg;
69 
70         rc = mbox_create_read_window(tctx, pos, (WINDOW_SIZE / BLOCK_SIZE));
71         assert(rc == 1);
72 
73         mbox_rspcpy(tctx->ctx, msg);
74 
75         len = get_u16(&msg->args[2]);
76         pos = get_u16(&msg->args[4]) + len;
77     }
78 
79     windows_reset_all(tctx->ctx);
80 
81     rc = mbox_create_read_window(tctx, pos, (WINDOW_SIZE / BLOCK_SIZE));
82     assert(rc == 1);
83 
84     return 0;
85 }
86