GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: fx_partition_offset_calculate.c Lines: 47 108 43.5 %
Date: 2024-01-10 21:53:23 Branches: 50 118 42.4 %

Line Branch Exec Source
1
/**************************************************************************/
2
/*                                                                        */
3
/*       Copyright (c) Microsoft Corporation. All rights reserved.        */
4
/*                                                                        */
5
/*       This software is licensed under the Microsoft Software License   */
6
/*       Terms for Microsoft Azure RTOS. Full text of the license can be  */
7
/*       found in the LICENSE file at https://aka.ms/AzureRTOS_EULA       */
8
/*       and in the root directory of this software.                      */
9
/*                                                                        */
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
/*  RELEASE HISTORY                                                       */
117
/*                                                                        */
118
/*    DATE              NAME                      DESCRIPTION             */
119
/*                                                                        */
120
/*  05-19-2020     William E. Lamie         Initial Version 6.0           */
121
/*  09-30-2020     William E. Lamie         Modified comment(s),          */
122
/*                                            resulting in version 6.1    */
123
/*  04-02-2021     William E. Lamie         Modified comment(s),          */
124
/*                                            ignored signature check for */
125
/*                                            no partition situation,     */
126
/*                                            resulting in version 6.1.6  */
127
/*                                                                        */
128
/**************************************************************************/
129
28
UINT  _fx_partition_offset_calculate(void  *partition_sector, UINT partition,
130
                                     ULONG *partition_start, ULONG *partition_size)
131
{
132
133
FX_MEDIA_PARTITION  partition_table[4];
134
UINT                count;
135
ULONG64             total_sectors;
136
UCHAR               *partition_sector_ptr;
137
138
139
    /* Setup working pointer and initialize count.  */
140
28
    partition_sector_ptr =  partition_sector;
141
28
    count =  0;
142
143
    /* Check for a real boot sector instead of a partition table.  */
144

28
    if ((partition_sector_ptr[0] == 0xe9) || ((partition_sector_ptr[0] == 0xeb) && (partition_sector_ptr[2] == 0x90)))
145
    {
146
147
        /* Yes, a real boot sector could be present.  */
148
149
        /* See if there are good values for sectors per FAT.  */
150



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])
151
        {
152
153
            /* There are values for sectors per FAT.  */
154
155
            /* Determine if there is a total sector count.  */
156
20
            total_sectors =  0;
157
158

20
            if (partition_sector_ptr[0x13] || partition_sector_ptr[0x14])
159
            {
160
161
                /* Calculate the total sectors, FAT12/16.  */
162
13
                total_sectors =  (((ULONG) partition_sector_ptr[0x14]) << 8) | ((ULONG) partition_sector_ptr[0x13]);
163
            }
164


7
            else if (partition_sector_ptr[0x20] || partition_sector_ptr[0x21] || partition_sector_ptr[0x22] || partition_sector_ptr[0x23])
165
            {
166
167
                /* Calculate the total sectors, FAT32.  */
168
6
                total_sectors =  (((ULONG) partition_sector_ptr[0x23]) << 24) |
169
6
                                 (((ULONG) partition_sector_ptr[0x22]) << 16) |
170
6
                                 (((ULONG) partition_sector_ptr[0x21]) << 8)  |
171
6
                                 ((ULONG) partition_sector_ptr[0x20]);
172
            }
173
174
            /* Determine if there is a total sector count.  */
175
20
            if (total_sectors)
176
            {
177
178
19
                if (partition_start != FX_NULL)
179
                {
180
                    /* Return an offset of 0, size of boot record, and a successful status.  */
181
18
                    *partition_start =  0;
182
                }
183
184
                /* Determine if the total sectors is required.  */
185
19
                if (partition_size != FX_NULL)
186
                {
187
188
                    /* Return the total sectors.  */
189
18
                    *partition_size =  (ULONG)(total_sectors & 0xFFFFFFFF);
190
                }
191
192
                /* Return success!  */
193
19
                return(FX_SUCCESS);
194
            }
195
        }
196
#ifdef FX_ENABLE_EXFAT
197
        /* See if there are good values for sectors per exFAT.  */
198
        else if (partition_sector_ptr[0x0b] == 0 && partition_sector_ptr[0x0c] == 0)
199
        {
200
            /* There are values for sectors per exFAT.  */
201
202
            /* Calculate the total sectors.  */
203
            total_sectors = _fx_utility_64_unsigned_read(&partition_sector_ptr[FX_EF_VOLUME_LENGTH]);
204
205
            /* Determine if there is a total sector count.  */
206
            if (total_sectors)
207
            {
208
209
                if (partition_start != FX_NULL)
210
                {
211
                    /* Return an offset of 0, size of boot record, and a successful status.  */
212
                    *partition_start =  0;
213
                }
214
215
                /* Determine if the total sectors is required.  */
216
                if (partition_size != FX_NULL)
217
                {
218
219
                    if (total_sectors > 0xFFFFFFFF)
220
                    {
221
222
                        /* Overflow. Just return not found. */
223
                        return(FX_NOT_FOUND);
224
                    }
225
226
                    /* Return the total sectors.  */
227
                    *partition_size =  (ULONG)(total_sectors & 0xFFFFFFFF);
228
                }
229
230
                /* Return success!  */
231
                return(FX_SUCCESS);
232
            }
233
        }
234
#endif /* FX_ENABLE_EXFAT */
235
    }
236
237
    /* Check signature to make sure the buffer is valid.  */
238

9
    if ((partition_sector_ptr[510] != 0x55) || (partition_sector_ptr[511] != 0xAA))
239
    {
240
241
        /* Invalid, return an error.  */
242
2
        return(FX_NOT_FOUND);
243
    }
244
245
    /* Not bootable, look for specific partition.  */
246
7
    _fx_utility_partition_get(partition_table, &count, 0, partition_sector_ptr);
247
248
    /* Determine if return value is valid.  */
249
7
    if (partition >= count)
250
    {
251
252
        /* No, return an error.  */
253
5
        return(FX_NOT_FOUND);
254
    }
255
256
    /* Return the partition starting sector, if non-NULL.  */
257
2
    if (partition_start != FX_NULL)
258
    {
259
1
        *partition_start =  partition_table[partition].fx_media_part_start;
260
    }
261
262
    /* Return the partition size, if non-NULL.  */
263
2
    if (partition_size != FX_NULL)
264
    {
265
1
        *partition_size =  partition_table[partition].fx_media_part_size;
266
    }
267
268
    /* Return successful completion.  */
269
2
    return(FX_SUCCESS);
270
}
271
272
273
/**************************************************************************/
274
/*                                                                        */
275
/*  FUNCTION                                               RELEASE        */
276
/*                                                                        */
277
/*    _fx_utility_partition_get                           PORTABLE C      */
278
/*                                                           6.1.6        */
279
/*  AUTHOR                                                                */
280
/*                                                                        */
281
/*    William E. Lamie, Microsoft Corporation                             */
282
/*                                                                        */
283
/*  DESCRIPTION                                                           */
284
/*                                                                        */
285
/*    This function parses the partition sector and completes the         */
286
/*    supplied partition entry structure.                                 */
287
/*                                                                        */
288
/*  INPUT                                                                 */
289
/*                                                                        */
290
/*    partition_table                       Pointer to partition table    */
291
/*    count                                 Number of partitions found    */
292
/*    sector                                Base sector                   */
293
/*    sector_buffer                         Buffer containing partition   */
294
/*                                            table                       */
295
/*                                                                        */
296
/*  OUTPUT                                                                */
297
/*                                                                        */
298
/*    return status                                                       */
299
/*                                                                        */
300
/*  CALLS                                                                 */
301
/*                                                                        */
302
/*    None                                                                */
303
/*                                                                        */
304
/*  CALLED BY                                                             */
305
/*                                                                        */
306
/*    _fx_partition_offset_calculate        Calculate partition offset    */
307
/*                                                                        */
308
/*  RELEASE HISTORY                                                       */
309
/*                                                                        */
310
/*    DATE              NAME                      DESCRIPTION             */
311
/*                                                                        */
312
/*  05-19-2020     William E. Lamie         Initial Version 6.0           */
313
/*  09-30-2020     William E. Lamie         Modified comment(s),          */
314
/*                                            resulting in version 6.1    */
315
/*  04-02-2021     William E. Lamie         Modified comment(s),          */
316
/*                                            resulting in version 6.1.6  */
317
/*                                                                        */
318
/**************************************************************************/
319
7
UINT  _fx_utility_partition_get(FX_MEDIA_PARTITION *partition_table,
320
                                UINT *count, ULONG sector, UCHAR *sector_buffer)
321
{
322
323
UINT    i;
324
ULONG   base_sector, value;
325
326
    /* This parameter has not been supported yet. */
327
    FX_PARAMETER_NOT_USED(sector);
328
329
    /* Initialize base sector.  */
330
7
    base_sector =  0;
331
332
35
    for(i = 446; i <= 494; i+=16)
333
    {
334
28
        if (sector_buffer[i + 4] == 0) /* no partition entry here */
335
        {
336
337
21
            partition_table[*count].fx_media_part_start = 0;
338
21
            partition_table[*count].fx_media_part_size  = 0;
339
        }
340
        else
341
        {
342
343
7
            value =  (ULONG) sector_buffer[i + 8]; /* little endian start value */
344
7
            value =  (((ULONG) sector_buffer[i + 9]) << 8) | value;
345
7
            value =  (((ULONG) sector_buffer[i + 10]) << 16) | value;
346
7
            value =  (((ULONG) sector_buffer[i + 11]) << 24) | value;
347
7
            partition_table[*count].fx_media_part_start = value + base_sector;
348
349
7
            value =  (ULONG) sector_buffer[i + 12]; /* little endian size value */
350
7
            value =  (((ULONG) sector_buffer[i + 13]) << 8) | value;
351
7
            value =  (((ULONG) sector_buffer[i + 14]) << 16) | value;
352
7
            value =  (((ULONG) sector_buffer[i + 15]) << 24) | value;
353
7
            partition_table[*count].fx_media_part_size = value;
354
        }
355
356
28
        (*count)++;
357
    }
358
359
    /* Return success.  */
360
7
    return(FX_SUCCESS);
361
}
362
363
/**************************************************************************/
364
/*                                                                        */
365
/*  FUNCTION                                               RELEASE        */
366
/*                                                                        */
367
/*    _fx_partition_offset_calculate_extended             PORTABLE C      */
368
/*                                                           6.2.0        */
369
/*  AUTHOR                                                                */
370
/*                                                                        */
371
/*    Xiuwen Cai, Microsoft Corporation                                   */
372
/*                                                                        */
373
/*  DESCRIPTION                                                           */
374
/*                                                                        */
375
/*    This function calculates the sector offset to the specified         */
376
/*    partition.  The buffer containing the partition table is also       */
377
/*    supplied to this function.  If the buffer supplied is a boot        */
378
/*    record (which could be the case in non-partition systems), this     */
379
/*    function returns an offset of zero, the total sectors, and a        */
380
/*    successful status indicating that the buffer supplied is the boot   */
381
/*    record.  Otherwise, if a partition is found, this function returns  */
382
/*    the sector offset to its boot record along with a successful        */
383
/*    status. If the specified partition is not found or the buffer is    */
384
/*    not a partition table or boot record, this function returns an      */
385
/*    error.                                                              */
386
/*                                                                        */
387
/*    Note: Empty partitions have a FX_NOT_FOUND return code.             */
388
/*      Use partition index 0 to 3 for primary partition and index 4 to   */
389
/*      FX_MAX_PARTITION_COUNT for extended partition.                    */
390
/*                                                                        */
391
/*  INPUT                                                                 */
392
/*                                                                        */
393
/*    media_ptr                             Media control block pointer   */
394
/*    partition_sector                      Pointer to buffer containing  */
395
/*                                            either the partition table  */
396
/*                                            or the boot sector          */
397
/*    partition                             Desired partition             */
398
/*    partition_start                       Return partition start        */
399
/*    partition_size                        Return partition size         */
400
/*                                                                        */
401
/*  OUTPUT                                                                */
402
/*                                                                        */
403
/*    return status                                                       */
404
/*                                                                        */
405
/*  CALLS                                                                 */
406
/*                                                                        */
407
/*    _fx_utility_16_unsigned_read          Read a USHORT from memory     */
408
/*    _fx_utility_32_unsigned_read          Read a ULONG from memory      */
409
/*    _fx_utility_64_unsigned_read          Read a ULONG64 from memory    */
410
/*    Media driver                                                        */
411
/*                                                                        */
412
/*  CALLED BY                                                             */
413
/*                                                                        */
414
/*    Application Driver                                                  */
415
/*                                                                        */
416
/*  RELEASE HISTORY                                                       */
417
/*                                                                        */
418
/*    DATE              NAME                      DESCRIPTION             */
419
/*                                                                        */
420
/*  10-31-2022     Xiuwen Cai               Initial Version 6.2.0        */
421
/*                                                                        */
422
/**************************************************************************/
423
UINT  _fx_partition_offset_calculate_extended(FX_MEDIA *media_ptr, void  *partition_sector, UINT partition,
424
                                     ULONG *partition_start, ULONG *partition_size)
425
{
426
427
ULONG64             total_sectors;
428
UCHAR               *partition_sector_ptr;
429
UCHAR               partition_type;
430
UINT                i;
431
ULONG               base_sector;
432
ULONG               base_sector_extended;
433
434
435
    /* Setup working pointer.  */
436
    partition_sector_ptr =  partition_sector;
437
438
    /* Check for a real boot sector instead of a partition table.  */
439
    if ((partition_sector_ptr[0] == 0xe9) || ((partition_sector_ptr[0] == 0xeb) && (partition_sector_ptr[2] == 0x90)))
440
    {
441
442
        /* Yes, a real boot sector could be present.  */
443
444
        /* See if there are good values for sectors per FAT.  */
445
        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])
446
        {
447
448
            /* There are values for sectors per FAT.  */
449
450
            /* Get the total sectors, FAT12/16.  */
451
            total_sectors =  _fx_utility_16_unsigned_read(&partition_sector_ptr[FX_SECTORS]);
452
453
            if (total_sectors == 0)
454
            {
455
456
                /* Get the total sectors, FAT32.  */
457
                total_sectors = _fx_utility_32_unsigned_read(&partition_sector_ptr[FX_HUGE_SECTORS]);
458
            }
459
460
            /* Determine if there is a total sector count.  */
461
            if (total_sectors)
462
            {
463
464
                if (partition_start != FX_NULL)
465
                {
466
                    /* Return an offset of 0, size of boot record, and a successful status.  */
467
                    *partition_start =  0;
468
                }
469
470
                /* Determine if the total sectors is required.  */
471
                if (partition_size != FX_NULL)
472
                {
473
474
                    /* Return the total sectors.  */
475
                    *partition_size =  (ULONG)(total_sectors & 0xFFFFFFFF);
476
                }
477
478
                /* Return success!  */
479
                return(FX_SUCCESS);
480
            }
481
        }
482
#ifdef FX_ENABLE_EXFAT
483
        /* See if there are good values for sectors per exFAT.  */
484
        else if (partition_sector_ptr[0x0b] == 0 && partition_sector_ptr[0x0c] == 0)
485
        {
486
            /* There are values for sectors per exFAT.  */
487
488
            /* Calculate the total sectors.  */
489
            total_sectors = _fx_utility_64_unsigned_read(&partition_sector_ptr[FX_EF_VOLUME_LENGTH]);
490
491
            /* Determine if there is a total sector count.  */
492
            if (total_sectors)
493
            {
494
495
                if (partition_start != FX_NULL)
496
                {
497
                    /* Return an offset of 0, size of boot record, and a successful status.  */
498
                    *partition_start =  0;
499
                }
500
501
                /* Determine if the total sectors is required.  */
502
                if (partition_size != FX_NULL)
503
                {
504
505
                    if (total_sectors > 0xFFFFFFFF)
506
                    {
507
508
                        /* Overflow. Just return not found. */
509
                        return(FX_NOT_FOUND);
510
                    }
511
512
                    /* Return the total sectors.  */
513
                    *partition_size =  (ULONG)(total_sectors & 0xFFFFFFFF);
514
                }
515
516
                /* Return success!  */
517
                return(FX_SUCCESS);
518
            }
519
        }
520
#endif /* FX_ENABLE_EXFAT */
521
    }
522
523
    /* Check signature to make sure the buffer is valid.  */
524
    if ((partition_sector_ptr[510] != FX_SIG_BYTE_1) || (partition_sector_ptr[511] != FX_SIG_BYTE_2))
525
    {
526
527
        /* Invalid, return an error.  */
528
        return(FX_NOT_FOUND);
529
    }
530
531
    /* Not bootable, look for specific partition.  */
532
533
    /* Check if primary partitions are addressed.  */
534
    if (partition < 4)
535
    {
536
537
        /* Get partition type.  */
538
        partition_type =  partition_sector_ptr[FX_PARTITION_TABLE_OFFSET + partition * FX_PARTITION_ENTRY_SIZE + FX_PARTITION_TYPE_OFFSET];
539
540
        /* Check if there is a vaild partition.  */
541
        if (partition_type != FX_PARTITION_TYPE_FREE)
542
        {
543
544
            /* Return the partition starting sector, if non-NULL.  */
545
            if (partition_start != FX_NULL)
546
            {
547
                *partition_start = _fx_utility_32_unsigned_read(&partition_sector_ptr[FX_PARTITION_TABLE_OFFSET + partition * FX_PARTITION_ENTRY_SIZE + FX_PARTITION_LBA_OFFSET]);
548
            }
549
550
            /* Return the partition size, if non-NULL.  */
551
            if (partition_size != FX_NULL)
552
            {
553
                *partition_size =  _fx_utility_32_unsigned_read(&partition_sector_ptr[FX_PARTITION_TABLE_OFFSET + partition * FX_PARTITION_ENTRY_SIZE + FX_PARTITION_SECTORS_OFFSET]);
554
            }
555
556
            /* Return success!  */
557
            return(FX_SUCCESS);
558
        }
559
        else
560
        {
561
562
            /* Not partition here.  */
563
            return(FX_NOT_FOUND);
564
        }
565
    }
566
567
    /* Check for invalid parameter.  */
568
    if (partition > FX_MAX_PARTITION_COUNT)
569
    {
570
571
        /* Return error.  */
572
        return(FX_NOT_FOUND);
573
    }
574
575
    base_sector = 0;
576
577
    /* Loop to find the extended partition table.  */
578
    for(i = FX_PARTITION_TABLE_OFFSET; i <= FX_PARTITION_TABLE_OFFSET + 3 * FX_PARTITION_ENTRY_SIZE; i += FX_PARTITION_ENTRY_SIZE)
579
    {
580
581
        /* Get partition type.  */
582
        partition_type =  partition_sector_ptr[i + FX_PARTITION_TYPE_OFFSET];
583
        if (partition_type == FX_PARTITION_TYPE_EXTENDED || partition_type == FX_PARTITION_TYPE_EXTENDED_LBA)
584
        {
585
            base_sector =  _fx_utility_32_unsigned_read(&partition_sector_ptr[i + FX_PARTITION_LBA_OFFSET]);
586
            break;
587
        }
588
    }
589
590
    if (base_sector == 0)
591
    {
592
593
        /* No extended partition.  */
594
        return(FX_NOT_FOUND);
595
    }
596
597
    base_sector_extended = base_sector;
598
599
    for (i = 4; i <= partition; i++)
600
    {
601
602
        /* Read the partition sector from the device.  Build the read sector
603
            command.  */
604
        media_ptr -> fx_media_driver_request =          FX_DRIVER_READ;
605
        media_ptr -> fx_media_driver_status =           FX_IO_ERROR;
606
        media_ptr -> fx_media_driver_buffer =           partition_sector_ptr;
607
        media_ptr -> fx_media_driver_logical_sector =   base_sector;
608
        media_ptr -> fx_media_driver_sectors =          1;
609
        media_ptr -> fx_media_driver_sector_type =      FX_UNKNOWN_SECTOR;
610
        media_ptr -> fx_media_hidden_sectors =          0;
611
612
        /* Invoke the driver to read the sector.  */
613
        (media_ptr -> fx_media_driver_entry) (media_ptr);
614
615
        /* Determine if the sector was read correctly. */
616
        if (media_ptr -> fx_media_driver_status != FX_SUCCESS)
617
        {
618
619
            /* Return error.  */
620
            return(FX_IO_ERROR);
621
        }
622
623
        /* Check signature to make sure the sector is valid.  */
624
        if ((partition_sector_ptr[510] != FX_SIG_BYTE_1) || (partition_sector_ptr[511] != FX_SIG_BYTE_2))
625
        {
626
627
            /* Invalid, return an error.  */
628
            return(FX_NOT_FOUND);
629
        }
630
631
        /* Determine if this is the desired partition.  */
632
        if (i == partition)
633
        {
634
635
            /* Get partition type.  */
636
            partition_type =  partition_sector_ptr[FX_PARTITION_TABLE_OFFSET + FX_PARTITION_TYPE_OFFSET];
637
            if (partition_type != FX_PARTITION_TYPE_FREE)
638
            {
639
640
                /* Return the partition starting sector, if non-NULL.  */
641
                if (partition_start != FX_NULL)
642
                {
643
                    *partition_start = _fx_utility_32_unsigned_read(&partition_sector_ptr[FX_PARTITION_TABLE_OFFSET + FX_PARTITION_LBA_OFFSET]) + base_sector;
644
                }
645
646
                /* Return the partition size, if non-NULL.  */
647
                if (partition_size != FX_NULL)
648
                {
649
                    *partition_size =  _fx_utility_32_unsigned_read(&partition_sector_ptr[FX_PARTITION_TABLE_OFFSET + FX_PARTITION_SECTORS_OFFSET]);
650
                }
651
652
                /* Return success!  */
653
                return(FX_SUCCESS);
654
            }
655
            else
656
            {
657
                /* Not partition here.  */
658
                return(FX_NOT_FOUND);
659
            }
660
        }
661
        else
662
        {
663
664
            /* Get partition type.  */
665
            partition_type =  partition_sector_ptr[FX_PARTITION_TABLE_OFFSET + FX_PARTITION_ENTRY_SIZE + FX_PARTITION_TYPE_OFFSET];
666
            if (partition_type == FX_PARTITION_TYPE_EXTENDED || partition_type == FX_PARTITION_TYPE_EXTENDED_LBA)
667
            {
668
669
                /* Update sector number for next partition table.  */
670
                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;
671
            }
672
            else
673
            {
674
                /* No valid partition, get out of the loop.  */
675
                break;
676
            }
677
        }
678
679
    }
680
681
    /* Return error.  */
682
    return(FX_NOT_FOUND);
683
}