GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: fxe_media_open.c Lines: 24 24 100.0 %
Date: 2026-03-06 18:49:02 Branches: 26 26 100.0 %

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
/** FileX Component                                                       */
17
/**                                                                       */
18
/**   Media                                                               */
19
/**                                                                       */
20
/**************************************************************************/
21
/**************************************************************************/
22
23
#define FX_SOURCE_CODE
24
25
26
/* Include necessary system files.  */
27
28
#include "fx_api.h"
29
#include "fx_media.h"
30
#include "fx_system.h"
31
32
33
FX_CALLER_CHECKING_EXTERNS
34
35
36
/**************************************************************************/
37
/*                                                                        */
38
/*  FUNCTION                                               RELEASE        */
39
/*                                                                        */
40
/*    _fxe_media_open                                     PORTABLE C      */
41
/*                                                           6.1          */
42
/*  AUTHOR                                                                */
43
/*                                                                        */
44
/*    William E. Lamie, Microsoft Corporation                             */
45
/*                                                                        */
46
/*  DESCRIPTION                                                           */
47
/*                                                                        */
48
/*    This function checks for errors in the media open call.             */
49
/*                                                                        */
50
/*  INPUT                                                                 */
51
/*                                                                        */
52
/*    media_ptr                             Media control block pointer   */
53
/*    media_name                            Pointer to media name string  */
54
/*    media_driver                          Media driver entry function   */
55
/*    driver_info_ptr                       Optional information pointer  */
56
/*                                            supplied to media driver    */
57
/*    memory_ptr                            Pointer to memory used by the */
58
/*                                            FileX for this media.       */
59
/*    memory_size                           Size of media memory - must   */
60
/*                                            at least 512 bytes and      */
61
/*                                            one sector size.            */
62
/*    media_control_block_size              Size of FX_MEDIA structure    */
63
/*                                                                        */
64
/*  OUTPUT                                                                */
65
/*                                                                        */
66
/*    FX_PTR_ERROR                          One or more input parameters  */
67
/*                                            are NULL                    */
68
/*    status                                Actual completion status      */
69
/*                                                                        */
70
/*  CALLS                                                                 */
71
/*                                                                        */
72
/*    tx_thread_identify                    Get current thread            */
73
/*    tx_thread_preemption_change           Disable/restore preemption    */
74
/*    _fx_media_open                        Actual media open service     */
75
/*                                                                        */
76
/*  CALLED BY                                                             */
77
/*                                                                        */
78
/*    Application Code                                                    */
79
/*                                                                        */
80
/**************************************************************************/
81
8616
UINT  _fxe_media_open(FX_MEDIA *media_ptr, CHAR *media_name,
82
                      VOID (*media_driver)(FX_MEDIA *), VOID *driver_info_ptr,
83
                      VOID *memory_ptr, ULONG memory_size, UINT media_control_block_size)
84
{
85
86
UINT       status;
87
ULONG      temp;
88
FX_MEDIA  *current_media;
89
ULONG      open_count;
90
91
#ifndef FX_SINGLE_THREAD
92
TX_THREAD *current_thread;
93
UINT       old_threshold;
94
#endif
95
96
97
    /* Check for invalid input pointers.  */
98


8616
    if ((media_ptr == FX_NULL) || (media_driver == FX_NULL) || (memory_ptr == FX_NULL) || (media_control_block_size != sizeof(FX_MEDIA)))
99
    {
100
409
        return(FX_PTR_ERROR);
101
    }
102
103
    /* Check for a valid caller.  */
104

8207
    FX_CALLER_CHECKING_CODE
105
106
    /* Check for proper size of the logical sector cache.  */
107
7799
    temp =  _fx_system_media_max_sector_cache;
108
109
    /* Isolate the lowest set bit.  */
110
7799
    temp =  (temp & ((~temp) + ((ULONG) 1)));
111
112
    /* If FX_MAX_SECTOR_CACHE is a power of 2, the value of temp should be unchanged.  */
113

7799
    if ((temp == 1) || (temp != _fx_system_media_max_sector_cache))
114
    {
115
116
        /* Not a power of 2, return an error.  */
117
272
        return(FX_MEDIA_INVALID);
118
    }
119
120
    /* Check for proper size of the FAT cache.  */
121
7527
    temp =  _fx_system_media_max_fat_cache;
122
123
    /* Isolate the lowest set bit.  */
124
7527
    temp =  (temp & ((~temp) + ((ULONG) 1)));
125
126
    /* If FX_MAX_FAT_CACHE is a power of 2, the value of temp should be unchanged.  */
127

7527
    if ((temp == 1) || (temp != _fx_system_media_max_fat_cache))
128
    {
129
130
        /* Not a power of 2, return an error.  */
131
272
        return(FX_MEDIA_INVALID);
132
    }
133
134
#ifndef FX_SINGLE_THREAD
135
136
    /* Pickup current thread pointer. At this point we know the current thread pointer is non-null since
137
       it was checked by code in FX_CALLER_CHECKING_CODE macro.  */
138
7255
    current_thread =  tx_thread_identify();
139
140
    /* Disable preemption temporarily.  */
141
7255
    tx_thread_preemption_change(current_thread, 0, &old_threshold);
142
#endif
143
144
    /* Loop to check for the media already opened.  */
145
7255
    current_media =  _fx_system_media_opened_ptr;
146
7255
    open_count =     _fx_system_media_opened_count;
147
7260
    while (open_count--)
148
    {
149
150
        /* Is the new media pointer already open?  */
151
6
        if (media_ptr == current_media)
152
        {
153
154
#ifndef FX_SINGLE_THREAD
155
156
            /* Restore preemption.  */
157
1
            tx_thread_preemption_change(current_thread, old_threshold, &old_threshold);
158
#endif
159
160
            /* Duplicate media open, return an error!  */
161
1
            return(FX_PTR_ERROR);
162
        }
163
164
        /* Move to next entry.  */
165
5
        current_media =  current_media -> fx_media_opened_next;
166
    }
167
168
#ifndef FX_SINGLE_THREAD
169
170
    /* Restore preemption.  */
171
7254
    tx_thread_preemption_change(current_thread, old_threshold, &old_threshold);
172
#endif
173
174
    /* Call actual media open service.  */
175
7254
    status =  _fx_media_open(media_ptr, media_name, media_driver, driver_info_ptr,
176
                             memory_ptr, memory_size);
177
178
    /* Return status.  */
179
7254
    return(status);
180
}
181