GCC Code Coverage Report


Directory: common/src/
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 83.3% 20 / 0 / 24
Functions: 100.0% 1 / 0 / 1
Branches: 57.1% 8 / 0 / 14

lx_nand_flash_metadata_write.c
Line Branch Exec Source
1 /***************************************************************************
2 * Copyright (c) 2024 Microsoft Corporation
3 * Copyright (c) 2026-present Eclipse ThreadX contributors
4 *
5 * This program and the accompanying materials are made available under the
6 * terms of the MIT License which is available at
7 * https://opensource.org/licenses/MIT.
8 *
9 * SPDX-License-Identifier: MIT
10 **************************************************************************/
11
12
13 /**************************************************************************/
14 /**************************************************************************/
15 /** */
16 /** LevelX Component */
17 /** */
18 /** NAND Flash */
19 /** */
20 /**************************************************************************/
21 /**************************************************************************/
22
23 #define LX_SOURCE_CODE
24
25
26 /* Disable ThreadX error checking. */
27
28 #ifndef LX_DISABLE_ERROR_CHECKING
29 #define LX_DISABLE_ERROR_CHECKING
30 #endif
31
32
33 /* Include necessary system files. */
34
35 #include "lx_api.h"
36
37
38 /**************************************************************************/
39 /* */
40 /* FUNCTION RELEASE */
41 /* */
42 /* _lx_nand_flash_metadata_write PORTABLE C */
43 /* 6.2.1 */
44 /* AUTHOR */
45 /* */
46 /* Xiuwen Cai, Microsoft Corporation */
47 /* */
48 /* DESCRIPTION */
49 /* */
50 /* This function writes metadata pages into current metadata block and */
51 /* allocates new blocks for metadata. */
52 /* */
53 /* INPUT */
54 /* */
55 /* nand_flash NAND flash instance */
56 /* main_buffer Main page buffer */
57 /* spare_value Value for spare bytes */
58 /* */
59 /* OUTPUT */
60 /* */
61 /* return status */
62 /* */
63 /* CALLS */
64 /* */
65 /* lx_nand_flash_driver_pages_write Driver pages write */
66 /* _lx_nand_flash_metadata_allocate Allocate blocks for metadata */
67 /* _lx_nand_flash_system_error Internal system error handler */
68 /* */
69 /* CALLED BY */
70 /* */
71 /* Internal LevelX */
72 /* */
73 /**************************************************************************/
74 369460 UINT _lx_nand_flash_metadata_write(LX_NAND_FLASH *nand_flash, UCHAR* main_buffer, ULONG spare_value)
75 {
76
77 ULONG block;
78 ULONG page;
79 UINT status;
80 UCHAR *spare_buffer_ptr;
81
82
83 /* Setup spare buffer pointer. */
84 369460 spare_buffer_ptr = nand_flash -> lx_nand_flash_page_buffer + nand_flash -> lx_nand_flash_bytes_per_page;
85
86 /* Initialize the spare buffer. */
87
2/2
✓ Branch 4 → 3 taken 4137952 times.
✓ Branch 4 → 5 taken 258622 times.
4507412 LX_MEMSET(spare_buffer_ptr, 0xFF, nand_flash -> lx_nand_flash_spare_total_length);
88
89 /* Check if there is enough spare data for metadata block number. */
90
2/4
✓ Branch 2 → 3 taken 110838 times.
✗ Branch 2 → 4 not taken.
✓ Branch 5 → 6 taken 258622 times.
✗ Branch 5 → 7 not taken.
369460 if (nand_flash -> lx_nand_flash_spare_data2_length >= 2)
91 {
92
93 /* Save metadata block number in spare bytes. */
94 369460 LX_UTILITY_SHORT_SET(&spare_buffer_ptr[nand_flash -> lx_nand_flash_spare_data2_offset], nand_flash -> lx_nand_flash_metadata_block_number);
95 }
96
97 /* Save metadata type data in spare bytes. */
98 369460 LX_UTILITY_LONG_SET(&spare_buffer_ptr[nand_flash -> lx_nand_flash_spare_data1_offset], spare_value);
99
100 /* Get current metadata block number. */
101 369460 block = nand_flash -> lx_nand_flash_metadata_block_number_current;
102
103 /* Get current metadata page number. */
104 369460 page = nand_flash -> lx_nand_flash_metadata_block_current_page;
105
106 /* Write the page. */
107 #ifdef LX_NAND_ENABLE_CONTROL_BLOCK_FOR_DRIVER_INTERFACE
108 36946 status = (nand_flash -> lx_nand_flash_driver_pages_write)(nand_flash, block, page, main_buffer, spare_buffer_ptr, 1);
109 #else
110 332514 status = (nand_flash -> lx_nand_flash_driver_pages_write)(block, page, main_buffer, spare_buffer_ptr, 1);
111 #endif
112
113 /* Check for an error from flash driver. */
114
2/4
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 8 taken 110838 times.
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 11 taken 258622 times.
369460 if (status)
115 {
116
117 /* Call system error handler. */
118 ✗ _lx_nand_flash_system_error(nand_flash, status, block, 0);
119
120 /* Return an error. */
121 ✗ return(status);
122 }
123
124 /* Increase current page for metadata block. */
125 369460 nand_flash -> lx_nand_flash_metadata_block_current_page++;
126
127 /* Get current backup metadata block number. */
128 369460 block = nand_flash -> lx_nand_flash_backup_metadata_block_number_current;
129
130 /* Get current backup metadata page number. */
131 369460 page = nand_flash -> lx_nand_flash_backup_metadata_block_current_page;
132
133 /* Write the page. */
134 #ifdef LX_NAND_ENABLE_CONTROL_BLOCK_FOR_DRIVER_INTERFACE
135 36946 status = (nand_flash -> lx_nand_flash_driver_pages_write)(nand_flash, block, page, main_buffer, spare_buffer_ptr, 1);
136 #else
137 332514 status = (nand_flash -> lx_nand_flash_driver_pages_write)(block, page, main_buffer, spare_buffer_ptr, 1);
138 #endif
139
140 /* Check for an error from flash driver. */
141
2/4
✗ Branch 9 → 10 not taken.
✓ Branch 9 → 12 taken 110838 times.
✗ Branch 12 → 13 not taken.
✓ Branch 12 → 15 taken 258622 times.
369460 if (status)
142 {
143
144 /* Call system error handler. */
145 ✗ _lx_nand_flash_system_error(nand_flash, status, block, 0);
146
147 /* Return an error. */
148 ✗ return(status);
149 }
150
151 /* Increase current page for backup metadata block. */
152 369460 nand_flash -> lx_nand_flash_backup_metadata_block_current_page++;
153
154 /* Allocate new block for metadata if necessary. */
155 369460 _lx_nand_flash_metadata_allocate(nand_flash);
156
157 /* Return sector not found status. */
158 369460 return(status);
159 }
160
161