GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: fx_partition_offset_calculate.c Lines: 47 108 43.5 %
Date: 2026-03-06 18:49:02 Branches: 50 118 42.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
/** FileX Component                                                       */
17
/**                                                                       */
18
/**   Application Utility                                                 */
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_utility.h"
30
31
32
/* Define internal data structures.  */
33
34
typedef struct FX_MEDIA_PARTITION_STRUCT
35
{
36
    ULONG fx_media_part_start;
37
    ULONG fx_media_part_size;
38
} FX_MEDIA_PARTITION;
39
40
/* Define internal partition constants. */
41
42
#ifndef FX_MAX_PARTITION_COUNT
43
#define FX_MAX_PARTITION_COUNT              16
44
#endif /* FX_MAX_PARTITION_COUNT */
45
46
#define FX_PARTITION_TABLE_OFFSET           446
47
#define FX_PARTITION_ENTRY_SIZE             16
48
#define FX_PARTITION_TYPE_OFFSET            4
49
#define FX_PARTITION_LBA_OFFSET             8
50
#define FX_PARTITION_SECTORS_OFFSET         12
51
52
#define FX_PARTITION_TYPE_FREE              0x00
53
#define FX_PARTITION_TYPE_EXTENDED          0x05
54
#define FX_PARTITION_TYPE_EXTENDED_LBA      0x0F
55
56
57
/* Define function prototypes for the partition table parsing application
58
   utility.  */
59
60
UINT    _fx_partition_offset_calculate(void  *partition_sector, UINT partition,
61
                                     ULONG *partition_start, ULONG *partition_size);
62
UINT    _fx_utility_partition_get(FX_MEDIA_PARTITION *partition_table,
63
                                UINT *count, ULONG sector, UCHAR *sector_buffer);
64
UINT    _fx_partition_offset_calculate_extended(FX_MEDIA *media_ptr, void  *partition_sector, UINT partition,
65
                                     ULONG *partition_start, ULONG *partition_size);
66
67
/**************************************************************************/
68
/*                                                                        */
69
/*  FUNCTION                                               RELEASE        */
70
/*                                                                        */
71
/*    _fx_partition_offset_calculate                      PORTABLE C      */
72
/*                                                           6.1.6        */
73
/*  AUTHOR                                                                */
74
/*                                                                        */
75
/*    William E. Lamie, Microsoft Corporation                             */
76
/*                                                                        */
77
/*  DESCRIPTION                                                           */
78
/*                                                                        */
79
/*    This function calculates the sector offset to the specified         */
80
/*    partition.  The buffer containing the partition table is also       */
81
/*    supplied to this function.  If the buffer supplied is a boot        */
82
/*    record (which could be the case in non-partition systems), this     */
83
/*    function returns an offset of zero, the total sectors, and a        */
84
/*    successful status indicating that the buffer supplied is the boot   */
85
/*    record.  Otherwise, if a partition is found, this function returns  */
86
/*    the sector offset to its boot record along with a successful        */
87
/*    status. If the specified partition is not found or the buffer is    */
88
/*    not a partition table or boot record, this function returns an      */
89
/*    error.                                                              */
90
/*                                                                        */
91
/*    Note: Empty partitions have a FX_SUCCESS return code, however their */
92
/*          starting sector is FX_NULL and the size returned is 0.        */
93
/*                                                                        */
94
/*  INPUT                                                                 */
95
/*                                                                        */
96
/*    partition_sector                      Pointer to buffer containing  */
97
/*                                            either the partition table  */
98
/*                                            or the boot sector          */
99
/*    partition                             Desired partition             */
100
/*    partition_start                       Return partition start        */
101
/*    partition_size                        Return partition size         */
102
/*                                                                        */
103
/*  OUTPUT                                                                */
104
/*                                                                        */
105
/*    return status                                                       */
106
/*                                                                        */
107
/*  CALLS                                                                 */
108
/*                                                                        */
109
/*     _fx_utility_partition_get            Actual partition parsing      */
110
/*                                            routine                     */
111
/*                                                                        */
112
/*  CALLED BY                                                             */
113
/*                                                                        */
114
/*    Application Driver                                                  */
115
/*                                                                        */
116
/**************************************************************************/
117
28
UINT  _fx_partition_offset_calculate(void  *partition_sector, UINT partition,
118
                                     ULONG *partition_start, ULONG *partition_size)
119
{
120
121
FX_MEDIA_PARTITION partition_table[4];
122
UINT               count;
123
ULONG64            total_sectors;
124
UCHAR             *partition_sector_ptr;
125
126
127
    /* Setup working pointer and initialize count.  */
128
28
    partition_sector_ptr =  partition_sector;
129
28
    count =  0;
130
131
    /* Check for a real boot sector instead of a partition table.  */
132

28
    if ((partition_sector_ptr[0] == 0xe9) || ((partition_sector_ptr[0] == 0xeb) && (partition_sector_ptr[2] == 0x90)))
133
    {
134
135
        /* Yes, a real boot sector could be present.  */
136
137
        /* See if there are good values for sectors per FAT.  */
138



21
        if (partition_sector_ptr[0x16] || partition_sector_ptr[0x17] || partition_sector_ptr[0x24] || partition_sector_ptr[0x25] || partition_sector_ptr[0x26] || partition_sector_ptr[0x27])
139
        {
140
141
            /* There are values for sectors per FAT.  */
142
143
            /* Determine if there is a total sector count.  */
144
20
            total_sectors =  0;
145
146

20
            if (partition_sector_ptr[0x13] || partition_sector_ptr[0x14])
147
            {
148
149
                /* Calculate the total sectors, FAT12/16.  */
150
13
                total_sectors =  (((ULONG) partition_sector_ptr[0x14]) << 8) | ((ULONG) partition_sector_ptr[0x13]);
151
            }
152


7
            else if (partition_sector_ptr[0x20] || partition_sector_ptr[0x21] || partition_sector_ptr[0x22] || partition_sector_ptr[0x23])
153
            {
154
155
                /* Calculate the total sectors, FAT32.  */
156
6
                total_sectors =  (((ULONG) partition_sector_ptr[0x23]) << 24) |
157
6
                                 (((ULONG) partition_sector_ptr[0x22]) << 16) |
158
6
                                 (((ULONG) partition_sector_ptr[0x21]) << 8)  |
159
6
                                 ((ULONG) partition_sector_ptr[0x20]);
160
            }
161
162
            /* Determine if there is a total sector count.  */
163
20
            if (total_sectors)
164
            {
165
166
19
                if (partition_start != FX_NULL)
167
                {
168
                    /* Return an offset of 0, size of boot record, and a successful status.  */
169
18
                    *partition_start =  0;
170
                }
171
172
                /* Determine if the total sectors is required.  */
173
19
                if (partition_size != FX_NULL)
174
                {
175
176
                    /* Return the total sectors.  */
177
18
                    *partition_size =  (ULONG)(total_sectors & 0xFFFFFFFF);
178
                }
179
180
                /* Return success!  */
181
19
                return(FX_SUCCESS);
182
            }
183
        }
184
    }
185
186
    /* Check signature to make sure the buffer is valid.  */
187

9
    if ((partition_sector_ptr[510] != 0x55) || (partition_sector_ptr[511] != 0xAA))
188
    {
189
190
        /* Invalid, return an error.  */
191
2
        return(FX_NOT_FOUND);
192
    }
193
194
    /* Not bootable, look for specific partition.  */
195
7
    _fx_utility_partition_get(partition_table, &count, 0, partition_sector_ptr);
196
197
    /* Determine if return value is valid.  */
198
7
    if (partition >= count)
199
    {
200
201
        /* No, return an error.  */
202
5
        return(FX_NOT_FOUND);
203
    }
204
205
    /* Return the partition starting sector, if non-NULL.  */
206
2
    if (partition_start != FX_NULL)
207
    {
208
1
        *partition_start =  partition_table[partition].fx_media_part_start;
209
    }
210
211
    /* Return the partition size, if non-NULL.  */
212
2
    if (partition_size != FX_NULL)
213
    {
214
1
        *partition_size =  partition_table[partition].fx_media_part_size;
215
    }
216
217
    /* Return successful completion.  */
218
2
    return(FX_SUCCESS);
219
}
220
221
222
/**************************************************************************/
223
/*                                                                        */
224
/*  FUNCTION                                               RELEASE        */
225
/*                                                                        */
226
/*    _fx_utility_partition_get                           PORTABLE C      */
227
/*                                                           6.1.6        */
228
/*  AUTHOR                                                                */
229
/*                                                                        */
230
/*    William E. Lamie, Microsoft Corporation                             */
231
/*                                                                        */
232
/*  DESCRIPTION                                                           */
233
/*                                                                        */
234
/*    This function parses the partition sector and completes the         */
235
/*    supplied partition entry structure.                                 */
236
/*                                                                        */
237
/*  INPUT                                                                 */
238
/*                                                                        */
239
/*    partition_table                       Pointer to partition table    */
240
/*    count                                 Number of partitions found    */
241
/*    sector                                Base sector                   */
242
/*    sector_buffer                         Buffer containing partition   */
243
/*                                            table                       */
244
/*                                                                        */
245
/*  OUTPUT                                                                */
246
/*                                                                        */
247
/*    return status                                                       */
248
/*                                                                        */
249
/*  CALLS                                                                 */
250
/*                                                                        */
251
/*    None                                                                */
252
/*                                                                        */
253
/*  CALLED BY                                                             */
254
/*                                                                        */
255
/*    _fx_partition_offset_calculate        Calculate partition offset    */
256
/*                                                                        */
257
/**************************************************************************/
258
7
UINT  _fx_utility_partition_get(FX_MEDIA_PARTITION *partition_table,
259
                                UINT *count, ULONG sector, UCHAR *sector_buffer)
260
{
261
262
UINT  i;
263
ULONG base_sector, value;
264
265
    /* This parameter has not been supported yet. */
266
    FX_PARAMETER_NOT_USED(sector);
267
268
    /* Initialize base sector.  */
269
7
    base_sector =  0;
270
271
35
    for(i = 446; i <= 494; i+=16)
272
    {
273
28
        if (sector_buffer[i + 4] == 0) /* no partition entry here */
274
        {
275
276
21
            partition_table[*count].fx_media_part_start = 0;
277
21
            partition_table[*count].fx_media_part_size  = 0;
278
        }
279
        else
280
        {
281
282
7
            value =  (ULONG) sector_buffer[i + 8]; /* little endian start value */
283
7
            value =  (((ULONG) sector_buffer[i + 9]) << 8) | value;
284
7
            value =  (((ULONG) sector_buffer[i + 10]) << 16) | value;
285
7
            value =  (((ULONG) sector_buffer[i + 11]) << 24) | value;
286
7
            partition_table[*count].fx_media_part_start = value + base_sector;
287
288
7
            value =  (ULONG) sector_buffer[i + 12]; /* little endian size value */
289
7
            value =  (((ULONG) sector_buffer[i + 13]) << 8) | value;
290
7
            value =  (((ULONG) sector_buffer[i + 14]) << 16) | value;
291
7
            value =  (((ULONG) sector_buffer[i + 15]) << 24) | value;
292
7
            partition_table[*count].fx_media_part_size = value;
293
        }
294
295
28
        (*count)++;
296
    }
297
298
    /* Return success.  */
299
7
    return(FX_SUCCESS);
300
}
301
302
/**************************************************************************/
303
/*                                                                        */
304
/*  FUNCTION                                               RELEASE        */
305
/*                                                                        */
306
/*    _fx_partition_offset_calculate_extended             PORTABLE C      */
307
/*                                                           6.2.0        */
308
/*  AUTHOR                                                                */
309
/*                                                                        */
310
/*    Xiuwen Cai, Microsoft Corporation                                   */
311
/*                                                                        */
312
/*  DESCRIPTION                                                           */
313
/*                                                                        */
314
/*    This function calculates the sector offset to the specified         */
315
/*    partition.  The buffer containing the partition table is also       */
316
/*    supplied to this function.  If the buffer supplied is a boot        */
317
/*    record (which could be the case in non-partition systems), this     */
318
/*    function returns an offset of zero, the total sectors, and a        */
319
/*    successful status indicating that the buffer supplied is the boot   */
320
/*    record.  Otherwise, if a partition is found, this function returns  */
321
/*    the sector offset to its boot record along with a successful        */
322
/*    status. If the specified partition is not found or the buffer is    */
323
/*    not a partition table or boot record, this function returns an      */
324
/*    error.                                                              */
325
/*                                                                        */
326
/*    Note: Empty partitions have a FX_NOT_FOUND return code.             */
327
/*      Use partition index 0 to 3 for primary partition and index 4 to   */
328
/*      FX_MAX_PARTITION_COUNT for extended partition.                    */
329
/*                                                                        */
330
/*  INPUT                                                                 */
331
/*                                                                        */
332
/*    media_ptr                             Media control block pointer   */
333
/*    partition_sector                      Pointer to buffer containing  */
334
/*                                            either the partition table  */
335
/*                                            or the boot sector          */
336
/*    partition                             Desired partition             */
337
/*    partition_start                       Return partition start        */
338
/*    partition_size                        Return partition size         */
339
/*                                                                        */
340
/*  OUTPUT                                                                */
341
/*                                                                        */
342
/*    return status                                                       */
343
/*                                                                        */
344
/*  CALLS                                                                 */
345
/*                                                                        */
346
/*    _fx_utility_16_unsigned_read          Read a USHORT from memory     */
347
/*    _fx_utility_32_unsigned_read          Read a ULONG from memory      */
348
/*    _fx_utility_64_unsigned_read          Read a ULONG64 from memory    */
349
/*    Media driver                                                        */
350
/*                                                                        */
351
/*  CALLED BY                                                             */
352
/*                                                                        */
353
/*    Application Driver                                                  */
354
/*                                                                        */
355
/**************************************************************************/
356
UINT  _fx_partition_offset_calculate_extended(FX_MEDIA *media_ptr, void  *partition_sector, UINT partition,
357
                                     ULONG *partition_start, ULONG *partition_size)
358
{
359
360
ULONG64 total_sectors;
361
UCHAR  *partition_sector_ptr;
362
UCHAR   partition_type;
363
UINT    i;
364
ULONG   base_sector;
365
ULONG   base_sector_extended;
366
367
368
    /* Setup working pointer.  */
369
    partition_sector_ptr =  partition_sector;
370
371
    /* Check for a real boot sector instead of a partition table.  */
372
    if ((partition_sector_ptr[0] == 0xe9) || ((partition_sector_ptr[0] == 0xeb) && (partition_sector_ptr[2] == 0x90)))
373
    {
374
375
        /* Yes, a real boot sector could be present.  */
376
377
        /* See if there are good values for sectors per FAT.  */
378
        if (partition_sector_ptr[0x16] || partition_sector_ptr[0x17] || partition_sector_ptr[0x24] || partition_sector_ptr[0x25] || partition_sector_ptr[0x26] || partition_sector_ptr[0x27])
379
        {
380
381
            /* There are values for sectors per FAT.  */
382
383
            /* Get the total sectors, FAT12/16.  */
384
            total_sectors =  _fx_utility_16_unsigned_read(&partition_sector_ptr[FX_SECTORS]);
385
386
            if (total_sectors == 0)
387
            {
388
389
                /* Get the total sectors, FAT32.  */
390
                total_sectors = _fx_utility_32_unsigned_read(&partition_sector_ptr[FX_HUGE_SECTORS]);
391
            }
392
393
            /* Determine if there is a total sector count.  */
394
            if (total_sectors)
395
            {
396
397
                if (partition_start != FX_NULL)
398
                {
399
                    /* Return an offset of 0, size of boot record, and a successful status.  */
400
                    *partition_start =  0;
401
                }
402
403
                /* Determine if the total sectors is required.  */
404
                if (partition_size != FX_NULL)
405
                {
406
407
                    /* Return the total sectors.  */
408
                    *partition_size =  (ULONG)(total_sectors & 0xFFFFFFFF);
409
                }
410
411
                /* Return success!  */
412
                return(FX_SUCCESS);
413
            }
414
        }
415
    }
416
417
    /* Check signature to make sure the buffer is valid.  */
418
    if ((partition_sector_ptr[510] != FX_SIG_BYTE_1) || (partition_sector_ptr[511] != FX_SIG_BYTE_2))
419
    {
420
421
        /* Invalid, return an error.  */
422
        return(FX_NOT_FOUND);
423
    }
424
425
    /* Not bootable, look for specific partition.  */
426
427
    /* Check if primary partitions are addressed.  */
428
    if (partition < 4)
429
    {
430
431
        /* Get partition type.  */
432
        partition_type =  partition_sector_ptr[FX_PARTITION_TABLE_OFFSET + partition * FX_PARTITION_ENTRY_SIZE + FX_PARTITION_TYPE_OFFSET];
433
434
        /* Check if there is a vaild partition.  */
435
        if (partition_type != FX_PARTITION_TYPE_FREE)
436
        {
437
438
            /* Return the partition starting sector, if non-NULL.  */
439
            if (partition_start != FX_NULL)
440
            {
441
                *partition_start = _fx_utility_32_unsigned_read(&partition_sector_ptr[FX_PARTITION_TABLE_OFFSET + partition * FX_PARTITION_ENTRY_SIZE + FX_PARTITION_LBA_OFFSET]);
442
            }
443
444
            /* Return the partition size, if non-NULL.  */
445
            if (partition_size != FX_NULL)
446
            {
447
                *partition_size =  _fx_utility_32_unsigned_read(&partition_sector_ptr[FX_PARTITION_TABLE_OFFSET + partition * FX_PARTITION_ENTRY_SIZE + FX_PARTITION_SECTORS_OFFSET]);
448
            }
449
450
            /* Return success!  */
451
            return(FX_SUCCESS);
452
        }
453
        else
454
        {
455
456
            /* Not partition here.  */
457
            return(FX_NOT_FOUND);
458
        }
459
    }
460
461
    /* Check for invalid parameter.  */
462
    if (partition > FX_MAX_PARTITION_COUNT)
463
    {
464
465
        /* Return error.  */
466
        return(FX_NOT_FOUND);
467
    }
468
469
    base_sector = 0;
470
471
    /* Loop to find the extended partition table.  */
472
    for(i = FX_PARTITION_TABLE_OFFSET; i <= FX_PARTITION_TABLE_OFFSET + 3 * FX_PARTITION_ENTRY_SIZE; i += FX_PARTITION_ENTRY_SIZE)
473
    {
474
475
        /* Get partition type.  */
476
        partition_type =  partition_sector_ptr[i + FX_PARTITION_TYPE_OFFSET];
477
        if (partition_type == FX_PARTITION_TYPE_EXTENDED || partition_type == FX_PARTITION_TYPE_EXTENDED_LBA)
478
        {
479
            base_sector =  _fx_utility_32_unsigned_read(&partition_sector_ptr[i + FX_PARTITION_LBA_OFFSET]);
480
            break;
481
        }
482
    }
483
484
    if (base_sector == 0)
485
    {
486
487
        /* No extended partition.  */
488
        return(FX_NOT_FOUND);
489
    }
490
491
    base_sector_extended = base_sector;
492
493
    for (i = 4; i <= partition; i++)
494
    {
495
496
        /* Read the partition sector from the device.  Build the read sector
497
            command.  */
498
        media_ptr -> fx_media_driver_request =          FX_DRIVER_READ;
499
        media_ptr -> fx_media_driver_status =           FX_IO_ERROR;
500
        media_ptr -> fx_media_driver_buffer =           partition_sector_ptr;
501
        media_ptr -> fx_media_driver_logical_sector =   base_sector;
502
        media_ptr -> fx_media_driver_sectors =          1;
503
        media_ptr -> fx_media_driver_sector_type =      FX_UNKNOWN_SECTOR;
504
        media_ptr -> fx_media_hidden_sectors =          0;
505
506
        /* Invoke the driver to read the sector.  */
507
        (media_ptr -> fx_media_driver_entry) (media_ptr);
508
509
        /* Determine if the sector was read correctly. */
510
        if (media_ptr -> fx_media_driver_status != FX_SUCCESS)
511
        {
512
513
            /* Return error.  */
514
            return(FX_IO_ERROR);
515
        }
516
517
        /* Check signature to make sure the sector is valid.  */
518
        if ((partition_sector_ptr[510] != FX_SIG_BYTE_1) || (partition_sector_ptr[511] != FX_SIG_BYTE_2))
519
        {
520
521
            /* Invalid, return an error.  */
522
            return(FX_NOT_FOUND);
523
        }
524
525
        /* Determine if this is the desired partition.  */
526
        if (i == partition)
527
        {
528
529
            /* Get partition type.  */
530
            partition_type =  partition_sector_ptr[FX_PARTITION_TABLE_OFFSET + FX_PARTITION_TYPE_OFFSET];
531
            if (partition_type != FX_PARTITION_TYPE_FREE)
532
            {
533
534
                /* Return the partition starting sector, if non-NULL.  */
535
                if (partition_start != FX_NULL)
536
                {
537
                    *partition_start = _fx_utility_32_unsigned_read(&partition_sector_ptr[FX_PARTITION_TABLE_OFFSET + FX_PARTITION_LBA_OFFSET]) + base_sector;
538
                }
539
540
                /* Return the partition size, if non-NULL.  */
541
                if (partition_size != FX_NULL)
542
                {
543
                    *partition_size =  _fx_utility_32_unsigned_read(&partition_sector_ptr[FX_PARTITION_TABLE_OFFSET + FX_PARTITION_SECTORS_OFFSET]);
544
                }
545
546
                /* Return success!  */
547
                return(FX_SUCCESS);
548
            }
549
            else
550
            {
551
                /* Not partition here.  */
552
                return(FX_NOT_FOUND);
553
            }
554
        }
555
        else
556
        {
557
558
            /* Get partition type.  */
559
            partition_type =  partition_sector_ptr[FX_PARTITION_TABLE_OFFSET + FX_PARTITION_ENTRY_SIZE + FX_PARTITION_TYPE_OFFSET];
560
            if (partition_type == FX_PARTITION_TYPE_EXTENDED || partition_type == FX_PARTITION_TYPE_EXTENDED_LBA)
561
            {
562
563
                /* Update sector number for next partition table.  */
564
                base_sector =  _fx_utility_32_unsigned_read(&partition_sector_ptr[FX_PARTITION_TABLE_OFFSET + FX_PARTITION_ENTRY_SIZE + FX_PARTITION_LBA_OFFSET]) + base_sector_extended;
565
            }
566
            else
567
            {
568
                /* No valid partition, get out of the loop.  */
569
                break;
570
            }
571
        }
572
573
    }
574
575
    /* Return error.  */
576
    return(FX_NOT_FOUND);
577
}