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