1 // Copyright 2018 The Fuchsia Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "ndmp.h"
6
7 #if INC_NDM
8 #include <kprivate/fsprivate.h>
9
10 // Global Function Definitions
11
12 #if INC_FTL_NDM && INC_SECT_FTL
13 // ndmWrFatPartition: Write Master Boot Record to NDM FAT partition
14 //
15 // Inputs: ndm = pointer to NDM control block
16 // part_num = NDM partition number
17 //
18 // Returns: 0 on success, -1 on error
19 //
ndmWrFatPartition(NDM ndm,ui32 part_num)20 int ndmWrFatPartition(NDM ndm, ui32 part_num) {
21 FtlNdmVol ftl;
22 FatVol fat;
23 void* ftl_ndm;
24
25 // Assign user configurable parameters for FAT/FTL volume.
26 ftl.flags = 0;
27 ftl.cached_map_pages = 1;
28 fat.flags = 0;
29
30 // Specify the FAT type and cluster size used by format() calls.
31 fat.desired_sects_per_clust = 0; // cluster size
32 fat.desired_type = FATANY; // FAT type
33
34 // Add an FTL to this partition.
35 ftl_ndm = ndmAddFatFTL(ndm, part_num, &ftl, &fat);
36 if (ftl_ndm == NULL)
37 return -1;
38
39 // Write Master Boot Record with 1 partition to media.
40 if (FatWrPartition(&fat))
41 return -1;
42
43 // Remove FTL from this partition and return success.
44 FtlnFreeFTL(ftl_ndm);
45 return 0;
46 }
47 #endif // INC_FTL_NDM && INC_SECT_FTL
48
49 // ndmDelVol: Un-initialize a Blunk file system volume, or custom
50 // one, for a partition entry in the partition table
51 //
52 // Inputs: ndm = pointer to NDM control block
53 // part_num = partition number
54 //
55 // Returns: 0 on success, -1 on error
56 //
ndmDelVol(CNDM ndm,ui32 part_num)57 int ndmDelVol(CNDM ndm, ui32 part_num) {
58 const NDMPartition* part;
59
60 // Get handle to entry in partitions table. Return if error.
61 part = ndmGetPartition(ndm, part_num);
62 if (part == NULL)
63 return -1;
64
65 // Based on partition type, either perform Blunk un-initialization,
66 // or custom one if present.
67 switch (part->type) {
68 #if INC_FFS_NDM
69 case FFS_VOL:
70 // Remove partition's TargetFFS volume. Return status.
71 return FfsDelVol(part->name);
72 #endif
73
74 #if INC_FTL_NDM_MLC || INC_FTL_NDM_SLC
75 case FAT_VOL:
76 case XFS_VOL:
77 // Remove partition's FTL and FAT or XFS volume. Return status.
78 return FtlNdmDelVol(part->name);
79 #endif
80
81
82 // This is where additional custom type cases could be added
83 }
84
85 // Return error if partition type is not handled.
86 return -1;
87 }
88
89 // ndmDelVols: Loop through partition table un-initializing valid
90 // partitions
91 //
92 // Input: ndm = pointer to NDM control block
93 //
94 // Returns: 0 on success, -1 on failure
95 //
ndmDelVols(CNDM ndm)96 int ndmDelVols(CNDM ndm) {
97 ui32 i, num_partitions;
98 int status = 0;
99
100 // Get the total number of partitions.
101 num_partitions = ndmGetNumPartitions(ndm);
102
103 // Loop through all partitions, un-initializing valid ones.
104 for (i = 0; i < num_partitions; ++i)
105 if (ndmDelVol(ndm, i))
106 status = -1;
107
108 // Return status.
109 return status;
110 }
111 #endif // INC_NDM
112