GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: lx_nand_flash_metadata_build.c Lines: 31 35 88.6 %
Date: 2026-03-06 18:45:40 Branches: 10 14 71.4 %

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
50
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
50
    nand_device_info_page = (LX_NAND_DEVICE_INFO*)nand_flash -> lx_nand_flash_page_buffer;
79
50
    nand_device_info_page -> lx_nand_device_info_signature1 = LX_NAND_DEVICE_INFO_SIGNATURE1;
80
50
    nand_device_info_page -> lx_nand_device_info_signature2 = LX_NAND_DEVICE_INFO_SIGNATURE2;
81
50
    nand_device_info_page -> lx_nand_device_info_major_version = LEVELX_MAJOR_VERSION;
82
50
    nand_device_info_page -> lx_nand_device_info_minor_version = LEVELX_MINOR_VERSION;
83
50
    nand_device_info_page -> lx_nand_device_info_patch_version = LEVELX_PATCH_VERSION;
84
50
    nand_device_info_page -> lx_nand_device_info_metadata_block_number = nand_flash -> lx_nand_flash_metadata_block_number;
85
50
    nand_device_info_page -> lx_nand_device_info_backup_metadata_block_number = nand_flash -> lx_nand_flash_backup_metadata_block_number;
86
50
    nand_device_info_page -> lx_nand_device_info_base_erase_count = nand_flash -> lx_nand_flash_base_erase_count;
87
88
    /* Write metadata.  */
89
50
    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
50
    if (status != LX_SUCCESS)
93
    {
94
95
        /* Return error status.  */
96
        return(status);
97
    }
98
99
    /* Calculate page count for erase count table.  */
100
50
    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
150
    for (i = 0; i < page_count; i++)
104
    {
105
106
        /* Write erase count table.  */
107
100
        status = _lx_nand_flash_metadata_write(nand_flash, (UCHAR*)(nand_flash -> lx_nand_flash_erase_count_table +
108
100
                                                i * nand_flash -> lx_nand_flash_bytes_per_page),
109
100
                                                LX_NAND_PAGE_TYPE_ERASE_COUNT_TABLE | i);
110
111
        /* Check return status.  */
112
100
        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
50
    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
250
    for (i = 0; i < page_count; i++)
125
    {
126
127
        /* Write block mapping table.  */
128
200
        status = _lx_nand_flash_metadata_write(nand_flash, (UCHAR*)(nand_flash -> lx_nand_flash_block_mapping_table +
129
200
                                                i * nand_flash -> lx_nand_flash_bytes_per_page / sizeof(*nand_flash -> lx_nand_flash_block_mapping_table)),
130
200
                                                LX_NAND_PAGE_TYPE_BLOCK_MAPPING_TABLE | i);
131
        /* Check return status.  */
132
200
        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
50
    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
250
    for (i = 0; i < page_count; i++)
145
    {
146
147
        /* Write block status table.  */
148
200
        status = _lx_nand_flash_metadata_write(nand_flash, (UCHAR*)(nand_flash -> lx_nand_flash_block_status_table +
149
200
                                            i * nand_flash -> lx_nand_flash_bytes_per_page / sizeof(*nand_flash -> lx_nand_flash_block_status_table)),
150
200
                                            LX_NAND_PAGE_TYPE_BLOCK_STATUS_TABLE | i);
151
152
        /* Check return status.  */
153
200
        if (status != LX_SUCCESS)
154
        {
155
156
            /* Return error status.  */
157
            return(status);
158
        }
159
    }
160
161
    /* Return status.  */
162
50
    return(status);
163
}
164