summaryrefslogtreecommitdiff
path: root/libparted/fs/fat/fat.c
blob: 4df802e65e11323ee4d1a1280007dc377b1c6e6d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
    libparted
    Copyright (C) 1998-2001, 2007-2014, 2019-2022 Free Software Foundation,
    Inc.

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <config.h>
#include <string.h>
#include <uuid/uuid.h>

#include "fat.h"

PedFileSystem*
fat_alloc (const PedGeometry* geom)
{
	PedFileSystem*		fs;

	fs = (PedFileSystem*) ped_malloc (sizeof (PedFileSystem));
	if (!fs)
		goto error;

	fs->type_specific = (FatSpecific*) ped_malloc (sizeof (FatSpecific));
	if (!fs->type_specific)
		goto error_free_fs;
	FatSpecific* fs_info = (FatSpecific*) fs->type_specific;
	fs_info->boot_sector = NULL;
	fs_info->info_sector = NULL;
	fs->geom = ped_geometry_duplicate (geom);
	if (!fs->geom)
		goto error_free_type_specific;

	fs->checked = 0;
	return fs;

error_free_type_specific:
	free (fs->type_specific);
error_free_fs:
	free (fs);
error:
	return NULL;
}

void
fat_free (PedFileSystem* fs)
{
	FatSpecific* fs_info = (FatSpecific*) fs->type_specific;
	free (fs_info->boot_sector);
	ped_geometry_destroy (fs->geom);
	free (fs->type_specific);
	free (fs);
}

PedGeometry*
fat_probe (PedGeometry* geom, FatType* fat_type)
{
	PedFileSystem*		fs;
	FatSpecific*		fs_info;
	PedGeometry*		result;

	fs = fat_alloc (geom);
	if (!fs)
		goto error;
	fs_info = (FatSpecific*) fs->type_specific;

	if (!fat_boot_sector_read (&fs_info->boot_sector, geom))
		goto error_free_fs;
	if (!fat_boot_sector_analyse (fs_info->boot_sector, fs))
		goto error_free_fs;

	*fat_type = fs_info->fat_type;
	result = ped_geometry_new (geom->dev, geom->start,
				   fs_info->sector_count);

	fat_free (fs);
	return result;

error_free_fs:
	fat_free (fs);
error:
	return NULL;
}

PedGeometry*
fat_probe_fat16 (PedGeometry* geom)
{
	FatType		fat_type;
	PedGeometry*	probed_geom = fat_probe (geom, &fat_type);

	if (probed_geom) {
		if (fat_type == FAT_TYPE_FAT16)
			return probed_geom;
		ped_geometry_destroy (probed_geom);
	}
	return NULL;
}

PedGeometry*
fat_probe_fat32 (PedGeometry* geom)
{
	FatType		fat_type;
	PedGeometry*	probed_geom = fat_probe (geom, &fat_type);

	if (probed_geom) {
		if (fat_type == FAT_TYPE_FAT32)
			return probed_geom;
		ped_geometry_destroy (probed_geom);
	}
	return NULL;
}

static PedFileSystemOps fat16_ops = {
	probe:		fat_probe_fat16,
};

static PedFileSystemOps fat32_ops = {
	probe:		fat_probe_fat32,
};

PedFileSystemType fat16_type = {
	next:	        NULL,
	ops:	        &fat16_ops,
	name:	        "fat16",
};

PedFileSystemType fat32_type = {
	next:	        NULL,
	ops:	        &fat32_ops,
	name:	        "fat32",
};

void
ped_file_system_fat_init ()
{
	if (sizeof (FatBootSector) != 512) {
		ped_exception_throw (PED_EXCEPTION_BUG, PED_EXCEPTION_CANCEL,
			_("GNU Parted was miscompiled: the FAT boot sector "
			"should be 512 bytes.  FAT support will be disabled."));
	} else {
		ped_file_system_type_register (&fat16_type);
		ped_file_system_type_register (&fat32_type);
	}
}

void
ped_file_system_fat_done ()
{
	ped_file_system_type_unregister (&fat16_type);
	ped_file_system_type_unregister (&fat32_type);
}