GCC Code Coverage Report


Directory: common/src/
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 64.0% 57 / 0 / 89
Functions: 100.0% 1 / 0 / 1
Branches: 69.7% 92 / 0 / 132

lx_nand_flash_sector_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 // Portions of this file were generated with AI assistance.
13
14 /**************************************************************************/
15 /**************************************************************************/
16 /** */
17 /** LevelX Component */
18 /** */
19 /** NAND Flash */
20 /** */
21 /**************************************************************************/
22 /**************************************************************************/
23
24 #define LX_SOURCE_CODE
25
26
27 /* Disable ThreadX error checking. */
28
29 #ifndef LX_DISABLE_ERROR_CHECKING
30 #define LX_DISABLE_ERROR_CHECKING
31 #endif
32
33
34 /* Include necessary system files. */
35
36 #include "lx_api.h"
37
38
39 /**************************************************************************/
40 /* */
41 /* FUNCTION RELEASE */
42 /* */
43 /* _lx_nand_flash_sector_write PORTABLE C */
44 /* 6.2.1 */
45 /* AUTHOR */
46 /* */
47 /* Xiuwen Cai, Microsoft Corporation */
48 /* */
49 /* DESCRIPTION */
50 /* */
51 /* This function writes a logical sector to the NAND flash page. */
52 /* */
53 /* INPUT */
54 /* */
55 /* nand_flash NAND flash instance */
56 /* logical_sector Logical sector number */
57 /* buffer Pointer to buffer to write */
58 /* (the size is number of */
59 /* bytes in a page) */
60 /* */
61 /* OUTPUT */
62 /* */
63 /* return status */
64 /* */
65 /* CALLS */
66 /* */
67 /* _lx_nand_flash_block_find Find the mapped block */
68 /* _lx_nand_flash_block_allocate Allocate block */
69 /* _lx_nand_flash_mapped_block_list_remove */
70 /* Remove mapped block */
71 /* _lx_nand_flash_data_page_copy Copy data pages */
72 /* _lx_nand_flash_free_block_list_add Add free block to list */
73 /* _lx_nand_flash_block_mapping_set Set block mapping */
74 /* lx_nand_flash_driver_pages_write Write pages */
75 /* _lx_nand_flash_block_status_set Set block status */
76 /* _lx_nand_flash_driver_block_erase Erase block */
77 /* _lx_nand_flash_erase_count_set Set erase count */
78 /* _lx_nand_flash_block_data_move Move block data */
79 /* _lx_nand_flash_mapped_block_list_add Add mapped block to list */
80 /* _lx_nand_flash_system_error Internal system error handler */
81 /* tx_mutex_get Get thread protection */
82 /* tx_mutex_put Release thread protection */
83 /* */
84 /* CALLED BY */
85 /* */
86 /* Application Code */
87 /* */
88 /**************************************************************************/
89 323000 UINT _lx_nand_flash_sector_write(LX_NAND_FLASH *nand_flash, ULONG logical_sector, VOID *buffer)
90 {
91
92 UINT status;
93 ULONG block;
94 ULONG new_block;
95 ULONG page;
96 323000 USHORT block_status = 0;
97 323000 USHORT new_block_status = LX_NAND_BLOCK_STATUS_ALLOCATED;
98 UCHAR *spare_buffer_ptr;
99 323000 UINT update_mapping = LX_FALSE;
100 323000 UINT copy_block = LX_FALSE;
101
102 #ifdef LX_THREAD_SAFE_ENABLE
103
104 /* Obtain the thread safe mutex. */
105 32300 tx_mutex_get(&nand_flash -> lx_nand_flash_mutex, TX_WAIT_FOREVER);
106 #endif
107
108 /* Increment the number of write requests. */
109 323000 nand_flash -> lx_nand_flash_diagnostic_sector_write_requests++;
110
111 /* See if we can find the logical sector in the current mapping. */
112 323000 status = _lx_nand_flash_block_find(nand_flash, logical_sector, &block, &block_status);
113
114 /* Check return status. */
115
2/4
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 7 taken 290700 times.
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 9 taken 32300 times.
323000 if(status != LX_SUCCESS)
116 {
117
118 /* Call system error handler. */
119 ✗ _lx_nand_flash_system_error(nand_flash, status, block, 0);
120
121 /* Determine if the error is fatal. */
122 ✗ if (status != LX_NAND_ERROR_CORRECTED)
123 {
124 #ifdef LX_THREAD_SAFE_ENABLE
125
126 /* Release the thread safe mutex. */
127 ✗ tx_mutex_put(&nand_flash -> lx_nand_flash_mutex);
128 #endif
129
130 /* Return an error. */
131 ✗ return(LX_ERROR);
132 }
133 }
134
135 #ifdef LX_NAND_FLASH_ENABLE_LAZY_SECTOR_RELEASE
136 /* If the block is full and has a pending compaction source, compact it now
137 before writing so the new data merges correctly with a single source block. */
138 if ((block != LX_NAND_BLOCK_UNMAPPED) && (block_status & LX_NAND_BLOCK_STATUS_FULL) &&
139 (nand_flash -> lx_nand_flash_block_compaction_table[logical_sector / nand_flash -> lx_nand_flash_pages_per_block] != (USHORT)LX_NAND_BLOCK_UNMAPPED))
140 {
141
142 status = _lx_nand_flash_logical_group_compact(nand_flash,
143 logical_sector / nand_flash -> lx_nand_flash_pages_per_block);
144
145 if (status)
146 {
147 _lx_nand_flash_system_error(nand_flash, status, block, 0);
148 #ifdef LX_THREAD_SAFE_ENABLE
149 tx_mutex_put(&nand_flash -> lx_nand_flash_mutex);
150 #endif
151 return(LX_ERROR);
152 }
153
154 /* Refresh block and block_status after compaction. */
155 status = _lx_nand_flash_block_find(nand_flash, logical_sector, &block, &block_status);
156
157 if (status != LX_SUCCESS)
158 {
159
160 if (status != LX_NAND_ERROR_CORRECTED)
161 {
162 #ifdef LX_THREAD_SAFE_ENABLE
163 tx_mutex_put(&nand_flash -> lx_nand_flash_mutex);
164 #endif
165 return(LX_ERROR);
166 }
167 }
168 }
169 #endif /* LX_NAND_FLASH_ENABLE_LAZY_SECTOR_RELEASE */
170
171 /* Check if block is unmapped or block is full. */
172
8/8
✓ Branch 7 → 8 taken 289053 times.
✓ Branch 7 → 9 taken 1647 times.
✓ Branch 8 → 9 taken 10260 times.
✓ Branch 8 → 19 taken 278793 times.
✓ Branch 9 → 10 taken 32117 times.
✓ Branch 9 → 11 taken 183 times.
✓ Branch 10 → 11 taken 1140 times.
✓ Branch 10 → 23 taken 30977 times.
323000 if (block == LX_NAND_BLOCK_UNMAPPED || block_status & LX_NAND_BLOCK_STATUS_FULL)
173 {
174
175 /* Allocate a new block. */
176 13230 status = _lx_nand_flash_block_allocate(nand_flash, &new_block);
177
178 /* Check if there is no blocks. */
179
2/4
✗ Branch 10 → 11 not taken.
✓ Branch 10 → 12 taken 11907 times.
✗ Branch 12 → 13 not taken.
✓ Branch 12 → 15 taken 1323 times.
13230 if (status == LX_NO_BLOCKS)
180 {
181 #ifdef LX_THREAD_SAFE_ENABLE
182
183 /* Release the thread safe mutex. */
184 ✗ tx_mutex_put(&nand_flash -> lx_nand_flash_mutex);
185 #endif
186
187 /* Return error. */
188 ✗ return(status);
189 }
190
191 /* Check return status. */
192
2/4
✗ Branch 12 → 13 not taken.
✓ Branch 12 → 16 taken 11907 times.
✗ Branch 15 → 16 not taken.
✓ Branch 15 → 20 taken 1323 times.
13230 else if (status != LX_SUCCESS)
193 {
194
195 /* Call system error handler. */
196 ✗ _lx_nand_flash_system_error(nand_flash, status, new_block, 0);
197
198 /* Determine if the error is fatal. */
199 ✗ if (status != LX_NAND_ERROR_CORRECTED)
200 {
201 #ifdef LX_THREAD_SAFE_ENABLE
202
203 /* Release the thread safe mutex. */
204 ✗ tx_mutex_put(&nand_flash -> lx_nand_flash_mutex);
205 #endif
206
207 /* Return an error. */
208 ✗ return(LX_ERROR);
209 }
210 }
211
212 /* Check if the block is full. */
213
4/4
✓ Branch 16 → 17 taken 10260 times.
✓ Branch 16 → 18 taken 1647 times.
✓ Branch 20 → 21 taken 1140 times.
✓ Branch 20 → 22 taken 183 times.
13230 if (block_status & LX_NAND_BLOCK_STATUS_FULL)
214 {
215
216 /* Set copy block flag. */
217 11400 copy_block = LX_TRUE;
218 }
219
220 /* Set update mapping flag. */
221 13230 update_mapping = LX_TRUE;
222 }
223 else
224 {
225
226 /* Set new block to the same as old block. */
227 309770 new_block = block;
228 309770 new_block_status = block_status;
229 }
230
231 /* Check if copy block flag is set. */
232
4/4
✓ Branch 20 → 21 taken 10260 times.
✓ Branch 20 → 26 taken 280440 times.
✓ Branch 24 → 25 taken 1140 times.
✓ Branch 24 → 31 taken 31160 times.
323000 if (copy_block)
233 {
234
235 /* Remove the old block from mapped block list. */
236 11400 _lx_nand_flash_mapped_block_list_remove(nand_flash, logical_sector / nand_flash -> lx_nand_flash_pages_per_block);
237
238 /* Copy valid sector to new block. */
239 11400 status = _lx_nand_flash_data_page_copy(nand_flash, logical_sector - (logical_sector % nand_flash -> lx_nand_flash_pages_per_block), block, block_status, new_block, &new_block_status, (logical_sector % nand_flash -> lx_nand_flash_pages_per_block));
240
241 /* Check for an error from flash driver. */
242
2/4
✗ Branch 23 → 24 not taken.
✓ Branch 23 → 26 taken 10260 times.
✗ Branch 27 → 28 not taken.
✓ Branch 27 → 31 taken 1140 times.
11400 if (status)
243 {
244
245 /* Call system error handler. */
246 ✗ _lx_nand_flash_system_error(nand_flash, status, block, 0);
247 #ifdef LX_THREAD_SAFE_ENABLE
248
249 /* Release the thread safe mutex. */
250 ✗ tx_mutex_put(&nand_flash -> lx_nand_flash_mutex);
251 #endif
252
253 /* Return an error. */
254 ✗ return(LX_ERROR);
255 }
256 }
257
258 /* Check if update mapping flag is set. */
259
4/4
✓ Branch 26 → 27 taken 11907 times.
✓ Branch 26 → 28 taken 278793 times.
✓ Branch 31 → 32 taken 1323 times.
✓ Branch 31 → 33 taken 30977 times.
323000 if (update_mapping)
260 {
261
262 /* Update block mapping. */
263 13230 _lx_nand_flash_block_mapping_set(nand_flash, logical_sector, new_block);
264 }
265
266 /* Setup spare buffer pointer. */
267 323000 spare_buffer_ptr = nand_flash -> lx_nand_flash_page_buffer;
268
269 /* Set spare buffer to all 0xFF bytes. */
270
4/4
✓ Branch 30 → 29 taken 3100800 times.
✓ Branch 30 → 31 taken 193800 times.
✓ Branch 35 → 34 taken 516800 times.
✓ Branch 35 → 36 taken 32300 times.
3940600 LX_MEMSET(spare_buffer_ptr, 0xFF, nand_flash -> lx_nand_flash_spare_total_length);
271
272 /* Check if there is enough spare data for metadata block number. */
273
3/6
✓ Branch 28 → 29 taken 96900 times.
✗ Branch 28 → 30 not taken.
✓ Branch 31 → 32 taken 193800 times.
✗ Branch 31 → 33 not taken.
✓ Branch 36 → 37 taken 32300 times.
✗ Branch 36 → 38 not taken.
323000 if (nand_flash -> lx_nand_flash_spare_data2_length >= sizeof(USHORT))
274 {
275
276 /* Save metadata block number in spare bytes. */
277 323000 LX_UTILITY_SHORT_SET(&spare_buffer_ptr[nand_flash -> lx_nand_flash_spare_data2_offset], nand_flash -> lx_nand_flash_metadata_block_number);
278 }
279
280 /* Set page type and sector address. */
281 323000 LX_UTILITY_LONG_SET(&spare_buffer_ptr[nand_flash -> lx_nand_flash_spare_data1_offset], LX_NAND_PAGE_TYPE_USER_DATA | logical_sector);
282
283 /* Get page to write. */
284 323000 page = new_block_status & LX_NAND_BLOCK_STATUS_PAGE_NUMBER_MASK;
285
286 /* Write the page. */
287 #ifdef LX_NAND_ENABLE_CONTROL_BLOCK_FOR_DRIVER_INTERFACE
288 32300 status = (nand_flash -> lx_nand_flash_driver_pages_write)(nand_flash, new_block, page, (UCHAR*)buffer, spare_buffer_ptr, 1);
289 #else
290 290700 status = (nand_flash -> lx_nand_flash_driver_pages_write)(new_block, page, (UCHAR*)buffer, spare_buffer_ptr, 1);
291 #endif
292
293 /* Check for an error from flash driver. */
294
3/6
✗ Branch 31 → 32 not taken.
✓ Branch 31 → 34 taken 96900 times.
✗ Branch 34 → 35 not taken.
✓ Branch 34 → 37 taken 193800 times.
✗ Branch 39 → 40 not taken.
✓ Branch 39 → 43 taken 32300 times.
323000 if (status)
295 {
296
297 /* Call system error handler. */
298 ✗ _lx_nand_flash_system_error(nand_flash, status, new_block, 0);
299 #ifdef LX_THREAD_SAFE_ENABLE
300
301 /* Release the thread safe mutex. */
302 ✗ tx_mutex_put(&nand_flash -> lx_nand_flash_mutex);
303 #endif
304
305 /* Return an error. */
306 ✗ return(LX_ERROR);
307 }
308
309 /* Determine if the sector number is sequential. */
310
6/6
✓ Branch 34 → 35 taken 29040 times.
✓ Branch 34 → 36 taken 67860 times.
✓ Branch 37 → 38 taken 58080 times.
✓ Branch 37 → 39 taken 135720 times.
✓ Branch 43 → 44 taken 9680 times.
✓ Branch 43 → 45 taken 22620 times.
323000 if ((new_block_status & LX_NAND_BLOCK_STATUS_PAGE_NUMBER_MASK) != (logical_sector % nand_flash -> lx_nand_flash_pages_per_block))
311 {
312
313 /* Set non sequential status flag. */
314 96800 new_block_status |= LX_NAND_BLOCK_STATUS_NON_SEQUENTIAL;
315 }
316
317 /* Increase page number. */
318 323000 page++;
319
320 /* Check if page number reaches total pages per block. */
321
6/6
✓ Branch 36 → 37 taken 270 times.
✓ Branch 36 → 38 taken 96630 times.
✓ Branch 39 → 40 taken 540 times.
✓ Branch 39 → 41 taken 193260 times.
✓ Branch 45 → 46 taken 90 times.
✓ Branch 45 → 47 taken 32210 times.
323000 if (page == nand_flash -> lx_nand_flash_pages_per_block)
322 {
323
324 /* Set block full status flag. */
325 900 new_block_status |= LX_NAND_BLOCK_STATUS_FULL;
326 }
327
328 /* Build block status word. */
329 323000 new_block_status = (USHORT)(page | (new_block_status & ~LX_NAND_BLOCK_STATUS_PAGE_NUMBER_MASK));
330
331 /* Determine if there are sectors after the addressed sector need to be copied. */
332
12/12
✓ Branch 38 → 39 taken 3420 times.
✓ Branch 38 → 44 taken 93480 times.
✓ Branch 39 → 40 taken 3402 times.
✓ Branch 39 → 44 taken 18 times.
✓ Branch 41 → 42 taken 6840 times.
✓ Branch 41 → 47 taken 186960 times.
✓ Branch 42 → 43 taken 6804 times.
✓ Branch 42 → 47 taken 36 times.
✓ Branch 47 → 48 taken 1140 times.
✓ Branch 47 → 54 taken 31160 times.
✓ Branch 48 → 49 taken 1134 times.
✓ Branch 48 → 54 taken 6 times.
323000 if (copy_block && ((logical_sector % nand_flash -> lx_nand_flash_pages_per_block) < (nand_flash -> lx_nand_flash_pages_per_block - 1)))
333 {
334
335 /* Copy valid sector to new block. */
336 11340 status = _lx_nand_flash_data_page_copy(nand_flash, logical_sector + 1, block, block_status, new_block, &new_block_status, (nand_flash -> lx_nand_flash_pages_per_block - 1) - (logical_sector % nand_flash -> lx_nand_flash_pages_per_block));
337
338 /* Check for an error from flash driver. */
339
3/6
✗ Branch 41 → 42 not taken.
✓ Branch 41 → 44 taken 3402 times.
✗ Branch 44 → 45 not taken.
✓ Branch 44 → 47 taken 6804 times.
✗ Branch 50 → 51 not taken.
✓ Branch 50 → 54 taken 1134 times.
11340 if (status)
340 {
341
342 /* Call system error handler. */
343 ✗ _lx_nand_flash_system_error(nand_flash, status, block, 0);
344 #ifdef LX_THREAD_SAFE_ENABLE
345
346 /* Release the thread safe mutex. */
347 ✗ tx_mutex_put(&nand_flash -> lx_nand_flash_mutex);
348 #endif
349
350 /* Return an error. */
351 ✗ return(LX_ERROR);
352 }
353 }
354
355 /* Set new block status. */
356 323000 status = _lx_nand_flash_block_status_set(nand_flash, new_block, new_block_status);
357
358 /* Check for an error from flash driver. */
359
3/6
✗ Branch 45 → 46 not taken.
✓ Branch 45 → 48 taken 96900 times.
✗ Branch 48 → 49 not taken.
✓ Branch 48 → 51 taken 193800 times.
✗ Branch 55 → 56 not taken.
✓ Branch 55 → 59 taken 32300 times.
323000 if (status)
360 {
361
362 /* Call system error handler. */
363 ✗ _lx_nand_flash_system_error(nand_flash, status, new_block, 0);
364 #ifdef LX_THREAD_SAFE_ENABLE
365
366 /* Release the thread safe mutex. */
367 ✗ tx_mutex_put(&nand_flash -> lx_nand_flash_mutex);
368 #endif
369
370 /* Return an error. */
371 ✗ return(LX_ERROR);
372 }
373
374 /* Check if copy block flag is set. */
375
6/6
✓ Branch 48 → 49 taken 3420 times.
✓ Branch 48 → 64 taken 93480 times.
✓ Branch 51 → 52 taken 6840 times.
✓ Branch 51 → 67 taken 186960 times.
✓ Branch 59 → 60 taken 1140 times.
✓ Branch 59 → 78 taken 31160 times.
323000 if (copy_block)
376 {
377
378 /* Erase old block. */
379 11400 status = _lx_nand_flash_driver_block_erase(nand_flash, block, nand_flash -> lx_nand_flash_base_erase_count + nand_flash -> lx_nand_flash_erase_count_table[block] + 1);
380
381 /* Check for an error from flash driver. */
382
3/6
✗ Branch 50 → 51 not taken.
✓ Branch 50 → 53 taken 3420 times.
✗ Branch 53 → 54 not taken.
✓ Branch 53 → 56 taken 6840 times.
✗ Branch 61 → 62 not taken.
✓ Branch 61 → 65 taken 1140 times.
11400 if (status)
383 {
384
385 /* Call system error handler. */
386 ✗ _lx_nand_flash_system_error(nand_flash, status, block, 0);
387 #ifdef LX_THREAD_SAFE_ENABLE
388
389 /* Release the thread safe mutex. */
390 ✗ tx_mutex_put(&nand_flash -> lx_nand_flash_mutex);
391 #endif
392
393 /* Return an error. */
394 ✗ return(LX_ERROR);
395 }
396
397 /* Update erase count for the old block. */
398 11400 status = _lx_nand_flash_erase_count_set(nand_flash, block, (UCHAR)(nand_flash -> lx_nand_flash_erase_count_table[block] + 1));
399
400 /* Check for an error from flash driver. */
401
3/6
✗ Branch 54 → 55 not taken.
✓ Branch 54 → 57 taken 3420 times.
✗ Branch 57 → 58 not taken.
✓ Branch 57 → 60 taken 6840 times.
✗ Branch 66 → 67 not taken.
✓ Branch 66 → 70 taken 1140 times.
11400 if (status)
402 {
403
404 /* Call system error handler. */
405 ✗ _lx_nand_flash_system_error(nand_flash, status, block, 0);
406 #ifdef LX_THREAD_SAFE_ENABLE
407
408 /* Release the thread safe mutex. */
409 ✗ tx_mutex_put(&nand_flash -> lx_nand_flash_mutex);
410 #endif
411
412 /* Return an error. */
413 ✗ return(LX_ERROR);
414 }
415
416 /* Check if the block has too many erases. */
417
3/6
✗ Branch 57 → 58 not taken.
✓ Branch 57 → 59 taken 3420 times.
✗ Branch 60 → 61 not taken.
✓ Branch 60 → 62 taken 6840 times.
✗ Branch 70 → 71 not taken.
✓ Branch 70 → 72 taken 1140 times.
11400 if (nand_flash -> lx_nand_flash_erase_count_table[block] > LX_NAND_FLASH_MAX_ERASE_COUNT_DELTA)
418 {
419
420 /* Move data from less worn block. */
421 ✗ _lx_nand_flash_block_data_move(nand_flash, block);
422 }
423 else
424 {
425
426 /* Set the block status to free. */
427 11400 status = _lx_nand_flash_block_status_set(nand_flash, block, LX_NAND_BLOCK_STATUS_FREE);
428
429 /* Check for an error from flash driver. */
430
3/6
✗ Branch 60 → 61 not taken.
✓ Branch 60 → 63 taken 3420 times.
✗ Branch 63 → 64 not taken.
✓ Branch 63 → 66 taken 6840 times.
✗ Branch 73 → 74 not taken.
✓ Branch 73 → 77 taken 1140 times.
11400 if (status)
431 {
432
433 /* Call system error handler. */
434 ✗ _lx_nand_flash_system_error(nand_flash, status, block, 0);
435 #ifdef LX_THREAD_SAFE_ENABLE
436
437 /* Release the thread safe mutex. */
438 ✗ tx_mutex_put(&nand_flash -> lx_nand_flash_mutex);
439 #endif
440
441 /* Return an error. */
442 ✗ return(LX_ERROR);
443 }
444
445 /* Add the block to free block list. */
446 11400 _lx_nand_flash_free_block_list_add(nand_flash, block);
447 }
448 }
449
450 /* Check if update mapping flag is set. */
451
6/6
✓ Branch 64 → 65 taken 3969 times.
✓ Branch 64 → 66 taken 92931 times.
✓ Branch 67 → 68 taken 7938 times.
✓ Branch 67 → 69 taken 185862 times.
✓ Branch 78 → 79 taken 1323 times.
✓ Branch 78 → 80 taken 30977 times.
323000 if (update_mapping)
452 {
453
454 /* Add the new block to mapped block list. */
455 13230 _lx_nand_flash_mapped_block_list_add(nand_flash, logical_sector / nand_flash -> lx_nand_flash_pages_per_block);
456 }
457
458 #ifdef LX_THREAD_SAFE_ENABLE
459
460 /* Release the thread safe mutex. */
461 32300 tx_mutex_put(&nand_flash -> lx_nand_flash_mutex);
462 #endif
463
464 /* Return the completion status. */
465 323000 return(status);
466 }
467
468
469