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