GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: lx_nand_flash_metadata_build.c Lines: 31 35 88.6 %
Date: 2024-03-11 05:20:25 Branches: 10 14 71.4 %

Line Branch Exec Source
1
/***************************************************************************
2
 * Copyright (c) 2024 Microsoft Corporation
3
 *
4
 * This program and the accompanying materials are made available under the
5
 * terms of the MIT License which is available at
6
 * https://opensource.org/licenses/MIT.
7
 *
8
 * SPDX-License-Identifier: MIT
9
 **************************************************************************/
10
11
12
/**************************************************************************/
13
/**************************************************************************/
14
/**                                                                       */
15
/** LevelX Component                                                      */
16
/**                                                                       */
17
/**   NAND Flash                                                          */
18
/**                                                                       */
19
/**************************************************************************/
20
/**************************************************************************/
21
22
#define LX_SOURCE_CODE
23
24
25
/* Disable ThreadX error checking.  */
26
27
#ifndef LX_DISABLE_ERROR_CHECKING
28
#define LX_DISABLE_ERROR_CHECKING
29
#endif
30
31
32
/* Include necessary system files.  */
33
34
#include "lx_api.h"
35
36
37
/**************************************************************************/
38
/*                                                                        */
39
/*  FUNCTION                                               RELEASE        */
40
/*                                                                        */
41
/*    _lx_nand_flash_metadata_build                       PORTABLE C      */
42
/*                                                           6.2.1       */
43
/*  AUTHOR                                                                */
44
/*                                                                        */
45
/*    Xiuwen Cai, Microsoft Corporation                                   */
46
/*                                                                        */
47
/*  DESCRIPTION                                                           */
48
/*                                                                        */
49
/*    This function rewrites all metadata pages.                          */
50
/*                                                                        */
51
/*  INPUT                                                                 */
52
/*                                                                        */
53
/*    nand_flash                            NAND flash instance           */
54
/*                                                                        */
55
/*  OUTPUT                                                                */
56
/*                                                                        */
57
/*    return status                                                       */
58
/*                                                                        */
59
/*  CALLS                                                                 */
60
/*                                                                        */
61
/*    _lx_nand_flash_metadata_write         Write metadata                */
62
/*                                                                        */
63
/*  CALLED BY                                                             */
64
/*                                                                        */
65
/*    Internal LevelX                                                     */
66
/*                                                                        */
67
/*  RELEASE HISTORY                                                       */
68
/*                                                                        */
69
/*    DATE              NAME                      DESCRIPTION             */
70
/*                                                                        */
71
/*  03-08-2023     Xiuwen Cai               Initial Version 6.2.1        */
72
/*                                                                        */
73
/**************************************************************************/
74
50
UINT  _lx_nand_flash_metadata_build(LX_NAND_FLASH *nand_flash)
75
{
76
77
UINT                status;
78
LX_NAND_DEVICE_INFO *nand_device_info_page;
79
UINT                page_count;
80
UINT                i;
81
82
    /* Build device info page.  */
83
50
    nand_device_info_page = (LX_NAND_DEVICE_INFO*)nand_flash -> lx_nand_flash_page_buffer;
84
50
    nand_device_info_page -> lx_nand_device_info_signature1 = LX_NAND_DEVICE_INFO_SIGNATURE1;
85
50
    nand_device_info_page -> lx_nand_device_info_signature2 = LX_NAND_DEVICE_INFO_SIGNATURE2;
86
50
    nand_device_info_page -> lx_nand_device_info_major_version = LEVELX_MAJOR_VERSION;
87
50
    nand_device_info_page -> lx_nand_device_info_minor_version = LEVELX_MINOR_VERSION;
88
50
    nand_device_info_page -> lx_nand_device_info_patch_version = LEVELX_PATCH_VERSION;
89
50
    nand_device_info_page -> lx_nand_device_info_metadata_block_number = nand_flash -> lx_nand_flash_metadata_block_number;
90
50
    nand_device_info_page -> lx_nand_device_info_backup_metadata_block_number = nand_flash -> lx_nand_flash_backup_metadata_block_number;
91
50
    nand_device_info_page -> lx_nand_device_info_base_erase_count = nand_flash -> lx_nand_flash_base_erase_count;
92
93
    /* Write metadata.  */
94
50
    status = _lx_nand_flash_metadata_write(nand_flash, (UCHAR*)nand_device_info_page, LX_NAND_PAGE_TYPE_DEVICE_INFO);
95
96
    /* Check return status.  */
97
50
    if (status != LX_SUCCESS)
98
    {
99
100
        /* Return error status.  */
101
        return(status);
102
    }
103
104
    /* Calculate page count for erase count table.  */
105
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;
106
107
    /* Loop to write all the pages.  */
108
150
    for (i = 0; i < page_count; i++)
109
    {
110
111
        /* Write erase count table.  */
112
100
        status = _lx_nand_flash_metadata_write(nand_flash, (UCHAR*)(nand_flash -> lx_nand_flash_erase_count_table +
113
100
                                                i * nand_flash -> lx_nand_flash_bytes_per_page),
114
100
                                                LX_NAND_PAGE_TYPE_ERASE_COUNT_TABLE | i);
115
116
        /* Check return status.  */
117
100
        if (status != LX_SUCCESS)
118
        {
119
120
            /* Return error status.  */
121
            return(status);
122
        }
123
    }
124
125
    /* Calculate page count for block mapping table.  */
126
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;
127
128
    /* Loop to write all the pages.  */
129
250
    for (i = 0; i < page_count; i++)
130
    {
131
132
        /* Write block mapping table.  */
133
200
        status = _lx_nand_flash_metadata_write(nand_flash, (UCHAR*)(nand_flash -> lx_nand_flash_block_mapping_table +
134
200
                                                i * nand_flash -> lx_nand_flash_bytes_per_page / sizeof(*nand_flash -> lx_nand_flash_block_mapping_table)),
135
200
                                                LX_NAND_PAGE_TYPE_BLOCK_MAPPING_TABLE | i);
136
        /* Check return status.  */
137
200
        if (status != LX_SUCCESS)
138
        {
139
140
            /* Return error status.  */
141
            return(status);
142
        }
143
    }
144
145
    /* Calculate page count for block status table.  */
146
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;
147
148
    /* Loop to write all the pages.  */
149
250
    for (i = 0; i < page_count; i++)
150
    {
151
152
        /* Write block status table.  */
153
200
        status = _lx_nand_flash_metadata_write(nand_flash, (UCHAR*)(nand_flash -> lx_nand_flash_block_status_table +
154
200
                                            i * nand_flash -> lx_nand_flash_bytes_per_page / sizeof(*nand_flash -> lx_nand_flash_block_status_table)),
155
200
                                            LX_NAND_PAGE_TYPE_BLOCK_STATUS_TABLE | i);
156
157
        /* Check return status.  */
158
200
        if (status != LX_SUCCESS)
159
        {
160
161
            /* Return error status.  */
162
            return(status);
163
        }
164
    }
165
166
    /* Return status.  */
167
50
    return(status);
168
}
169