1 /*
2  * Copyright (c) 2001-2019, Arm Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include "stdint.h"
8 #include <string.h>
9 #include <inttypes.h>
10 
11 #include "run_integration_pal_log.h"
12 #include "run_integration_flash.h"
13 #include "run_integration_helper.h"
14 
15 #include "test_pal_log.h"
16 
17 
18 /************************************************************
19  *
20  * static function prototypes
21  *
22  ************************************************************/
23 static RunItError_t runIt_flashValidateInput(uint32_t addr, size_t len, uint8_t* buff);
24 
25 /************************************************************
26  *
27  * variables
28  *
29  ************************************************************/
30 /** Holds the address of memory allocated for the flash chunk */
31 uint8_t *gFlashBaseAddr = NULL;
32 
33 /** Holds the size of the allocated Flash size. */
34 uint32_t gFlashSize = 0;
35 
36 /************************************************************
37  *
38  * static functions
39  *
40  ************************************************************/
runIt_flashValidateInput(uint32_t addr,size_t len,uint8_t * buff)41 static RunItError_t runIt_flashValidateInput(uint32_t addr, size_t len, uint8_t* buff)
42 {
43     int32_t remainder;
44 
45     /* validate null values */
46     if (buff == NULL)
47     {
48         RUNIT_PRINT_ERROR("output buffer is NULL\n");
49         return RUNIT_ERROR__FAIL;
50     }
51 
52     /* validate address in range */
53     remainder = ((int32_t)gFlashSize) - (addr + len);
54     if (remainder < 0)
55     {
56         RUNIT_PRINT_ERROR("Attempting to read behind flash partition by %"PRId32" bytes\n", remainder * (-1));
57         return RUNIT_ERROR__FAIL;
58     }
59 
60     return RUNIT_ERROR__OK;
61 }
62 
63 /************************************************************
64  *
65  * public functions
66  *
67  ************************************************************/
runIt_flashFinalize(void)68 RunItError_t runIt_flashFinalize(void)
69 {
70     if (gFlashBaseAddr != NULL)
71     {
72         RUNIT_PRINT_DBG("finialize flash\n");
73 
74         RUNIT_TEST_PAL_FREE(gFlashBaseAddr);
75         gFlashSize = 0;
76         gFlashBaseAddr = NULL;
77     }
78 
79     return RUNIT_ERROR__OK;
80 
81 }
82 
runIt_flashInit(size_t flashSize)83 RunItError_t runIt_flashInit(size_t flashSize)
84 {
85     /* if already allocated, print to log and return error */
86     if (gFlashBaseAddr != NULL)
87     {
88         RUNIT_PRINT_DBG("Attempting to initialize flash 2nd time\n");
89         return RUNIT_ERROR__OK;
90     }
91 
92     RUNIT_PRINT_DBG("init flash\n");
93 
94     /* allocate mem */
95     gFlashBaseAddr = (uint8_t*) RUNIT_TEST_PAL_ALLOC(flashSize);
96 
97     /* Validate */
98     if (gFlashBaseAddr == NULL)
99     {
100         RUNIT_PRINT_ERROR("Failed to allocate %zu bytes for flash\n", flashSize);
101         return RUNIT_ERROR__FAIL;
102     }
103 
104     gFlashSize = flashSize;
105 
106     return RUNIT_ERROR__OK;
107 }
108 
runIt_flashWrite(uint32_t addr,uint8_t * pBuff,size_t len)109 RunItError_t runIt_flashWrite(uint32_t addr, uint8_t* pBuff, size_t len)
110 {
111     RunItError_t rc = runIt_flashValidateInput(addr, len, pBuff);
112     if (rc != RUNIT_ERROR__OK)
113     {
114         RUNIT_PRINT_ERROR("Failed input validation\n");
115         return RUNIT_ERROR__FAIL;
116     }
117 
118     /* memcpy from buff */
119     memcpy(gFlashBaseAddr + addr, pBuff, len);
120 
121     return rc;
122 
123 }
124 
runIt_flashRead(uint32_t addr,uint8_t * pBuff,size_t len)125 RunItError_t runIt_flashRead(uint32_t addr, uint8_t* pBuff, size_t len)
126 {
127     RunItError_t rc = runIt_flashValidateInput(addr, len, pBuff);
128     if (rc != RUNIT_ERROR__OK)
129     {
130         RUNIT_PRINT_ERROR("Failed input validation\n");
131         return RUNIT_ERROR__FAIL;
132     }
133 
134     /* memcpy to buff */
135     memcpy(pBuff, gFlashBaseAddr + addr, len);
136 
137     return rc;
138 }
139 
140