lx_nand_flash_metadata_build.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_build PORTABLE C */ | ||
| 43 | /* 6.2.1 */ | ||
| 44 | /* AUTHOR */ | ||
| 45 | /* */ | ||
| 46 | /* Xiuwen Cai, Microsoft Corporation */ | ||
| 47 | /* */ | ||
| 48 | /* DESCRIPTION */ | ||
| 49 | /* */ | ||
| 50 | /* This function rewrites all metadata pages. */ | ||
| 51 | /* */ | ||
| 52 | /* INPUT */ | ||
| 53 | /* */ | ||
| 54 | /* nand_flash NAND flash instance */ | ||
| 55 | /* */ | ||
| 56 | /* OUTPUT */ | ||
| 57 | /* */ | ||
| 58 | /* return status */ | ||
| 59 | /* */ | ||
| 60 | /* CALLS */ | ||
| 61 | /* */ | ||
| 62 | /* _lx_nand_flash_metadata_write Write metadata */ | ||
| 63 | /* */ | ||
| 64 | /* CALLED BY */ | ||
| 65 | /* */ | ||
| 66 | /* Internal LevelX */ | ||
| 67 | /* */ | ||
| 68 | /**************************************************************************/ | ||
| 69 | 500 | UINT _lx_nand_flash_metadata_build(LX_NAND_FLASH *nand_flash) | |
| 70 | { | ||
| 71 | |||
| 72 | UINT status; | ||
| 73 | LX_NAND_DEVICE_INFO *nand_device_info_page; | ||
| 74 | UINT page_count; | ||
| 75 | UINT i; | ||
| 76 | |||
| 77 | /* Build device info page. */ | ||
| 78 | 500 | nand_device_info_page = (LX_NAND_DEVICE_INFO*)nand_flash -> lx_nand_flash_page_buffer; | |
| 79 | 500 | nand_device_info_page -> lx_nand_device_info_signature1 = LX_NAND_DEVICE_INFO_SIGNATURE1; | |
| 80 | 500 | nand_device_info_page -> lx_nand_device_info_signature2 = LX_NAND_DEVICE_INFO_SIGNATURE2; | |
| 81 | 500 | nand_device_info_page -> lx_nand_device_info_major_version = LEVELX_MAJOR_VERSION; | |
| 82 | 500 | nand_device_info_page -> lx_nand_device_info_minor_version = LEVELX_MINOR_VERSION; | |
| 83 | 500 | nand_device_info_page -> lx_nand_device_info_patch_version = LEVELX_PATCH_VERSION; | |
| 84 | 500 | nand_device_info_page -> lx_nand_device_info_metadata_block_number = nand_flash -> lx_nand_flash_metadata_block_number; | |
| 85 | 500 | nand_device_info_page -> lx_nand_device_info_backup_metadata_block_number = nand_flash -> lx_nand_flash_backup_metadata_block_number; | |
| 86 | 500 | nand_device_info_page -> lx_nand_device_info_base_erase_count = nand_flash -> lx_nand_flash_base_erase_count; | |
| 87 | |||
| 88 | /* Write metadata. */ | ||
| 89 | 500 | status = _lx_nand_flash_metadata_write(nand_flash, (UCHAR*)nand_device_info_page, LX_NAND_PAGE_TYPE_DEVICE_INFO); | |
| 90 | |||
| 91 | /* Check return status. */ | ||
| 92 |
1/2✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 500 times.
|
500 | if (status != LX_SUCCESS) |
| 93 | { | ||
| 94 | |||
| 95 | /* Return error status. */ | ||
| 96 | ✗ | return(status); | |
| 97 | } | ||
| 98 | |||
| 99 | /* Calculate page count for erase count table. */ | ||
| 100 | 500 | page_count = (nand_flash -> lx_nand_flash_erase_count_table_size + (nand_flash -> lx_nand_flash_bytes_per_page - 1)) / nand_flash -> lx_nand_flash_bytes_per_page; | |
| 101 | |||
| 102 | /* Loop to write all the pages. */ | ||
| 103 |
2/2✓ Branch 10 → 6 taken 1000 times.
✓ Branch 10 → 11 taken 500 times.
|
1500 | for (i = 0; i < page_count; i++) |
| 104 | { | ||
| 105 | |||
| 106 | /* Write erase count table. */ | ||
| 107 | 1000 | status = _lx_nand_flash_metadata_write(nand_flash, (UCHAR*)(nand_flash -> lx_nand_flash_erase_count_table + | |
| 108 | 1000 | i * nand_flash -> lx_nand_flash_bytes_per_page), | |
| 109 | 1000 | LX_NAND_PAGE_TYPE_ERASE_COUNT_TABLE | i); | |
| 110 | |||
| 111 | /* Check return status. */ | ||
| 112 |
1/2✗ Branch 7 → 8 not taken.
✓ Branch 7 → 9 taken 1000 times.
|
1000 | if (status != LX_SUCCESS) |
| 113 | { | ||
| 114 | |||
| 115 | /* Return error status. */ | ||
| 116 | ✗ | return(status); | |
| 117 | } | ||
| 118 | } | ||
| 119 | |||
| 120 | /* Calculate page count for block mapping table. */ | ||
| 121 | 500 | page_count = (nand_flash -> lx_nand_flash_block_mapping_table_size + (nand_flash -> lx_nand_flash_bytes_per_page - 1)) / nand_flash -> lx_nand_flash_bytes_per_page; | |
| 122 | |||
| 123 | /* Loop to write all the pages. */ | ||
| 124 |
2/2✓ Branch 16 → 12 taken 2000 times.
✓ Branch 16 → 17 taken 500 times.
|
2500 | for (i = 0; i < page_count; i++) |
| 125 | { | ||
| 126 | |||
| 127 | /* Write block mapping table. */ | ||
| 128 | 2000 | status = _lx_nand_flash_metadata_write(nand_flash, (UCHAR*)(nand_flash -> lx_nand_flash_block_mapping_table + | |
| 129 | 2000 | i * nand_flash -> lx_nand_flash_bytes_per_page / sizeof(*nand_flash -> lx_nand_flash_block_mapping_table)), | |
| 130 | 2000 | LX_NAND_PAGE_TYPE_BLOCK_MAPPING_TABLE | i); | |
| 131 | /* Check return status. */ | ||
| 132 |
1/2✗ Branch 13 → 14 not taken.
✓ Branch 13 → 15 taken 2000 times.
|
2000 | if (status != LX_SUCCESS) |
| 133 | { | ||
| 134 | |||
| 135 | /* Return error status. */ | ||
| 136 | ✗ | return(status); | |
| 137 | } | ||
| 138 | } | ||
| 139 | |||
| 140 | /* Calculate page count for block status table. */ | ||
| 141 | 500 | page_count = (nand_flash -> lx_nand_flash_block_status_table_size + (nand_flash -> lx_nand_flash_bytes_per_page - 1)) / nand_flash -> lx_nand_flash_bytes_per_page; | |
| 142 | |||
| 143 | /* Loop to write all the pages. */ | ||
| 144 |
2/2✓ Branch 22 → 18 taken 2000 times.
✓ Branch 22 → 23 taken 500 times.
|
2500 | for (i = 0; i < page_count; i++) |
| 145 | { | ||
| 146 | |||
| 147 | /* Write block status table. */ | ||
| 148 | 2000 | status = _lx_nand_flash_metadata_write(nand_flash, (UCHAR*)(nand_flash -> lx_nand_flash_block_status_table + | |
| 149 | 2000 | i * nand_flash -> lx_nand_flash_bytes_per_page / sizeof(*nand_flash -> lx_nand_flash_block_status_table)), | |
| 150 | 2000 | LX_NAND_PAGE_TYPE_BLOCK_STATUS_TABLE | i); | |
| 151 | |||
| 152 | /* Check return status. */ | ||
| 153 |
1/2✗ Branch 19 → 20 not taken.
✓ Branch 19 → 21 taken 2000 times.
|
2000 | if (status != LX_SUCCESS) |
| 154 | { | ||
| 155 | |||
| 156 | /* Return error status. */ | ||
| 157 | ✗ | return(status); | |
| 158 | } | ||
| 159 | } | ||
| 160 | |||
| 161 | /* Return status. */ | ||
| 162 | 500 | return(status); | |
| 163 | } | ||
| 164 | |||
| 165 |