summaryrefslogtreecommitdiff
path: root/libparted/fs/bfs/bfs.c
blob: 4a5c5baba033f5f3ef09cb4e3e97865df47c91eb (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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
    libparted - a library for manipulating disk partitions
    Copyright (C) 2005 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 2 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, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/

#include "config.h"

#include <parted/parted.h>
#include <parted/endian.h>
#include <parted/debug.h>

#if ENABLE_NLS
#  include <libintl.h>
#  define _(String) dgettext (PACKAGE, String)
#else
#  define _(String) (String)
#endif /* ENABLE_NLS */

#include <unistd.h>
#include <string.h>

#include "bfs.h"


#define BFS_SPECIFIC(fs) ((struct BfsSpecific*) (fs->type_specific))
#define BFS_SB(fs)       (BFS_SPECIFIC(fs)->sb)


const char BFS_MAGIC[4] = { 0x1B, 0xAD, 0xFA, 0xCE };
const long long BFS_SECTOR_SIZE         = 512;
const uint32_t  BFS_PED_SANITY          = 0xffffffff;
const long long BFS_PED_MIN_INODES      = 16;

static PedGeometry*
bfs_probe (PedGeometry* geom)
{
	uint8_t*        buf;

        PED_ASSERT (geom      != NULL, return NULL);
        PED_ASSERT (geom->dev != NULL, return NULL);

        buf = ped_malloc (geom->dev->sector_size);
        
	if (!ped_geometry_read (geom, buf, 0, 1))
		return 0;

        //if ( PED_CPU_TO_LE32((uint32_t)buf) == BFS_MAGIC )
		return ped_geometry_new (geom->dev, geom->start,
                                ped_div_round_up (
                                        PED_CPU_TO_LE32((uint32_t)(buf+8)),
                                        geom->dev->sector_size));
	else
		return NULL;
}

#ifndef DISCOVER_ONLY
static int
bfs_clobber (PedGeometry* geom)
{
	uint8_t*  buf;

        PED_ASSERT (geom      != NULL, return 0);
        PED_ASSERT (geom->dev != NULL, return 0);

        buf = ped_malloc (geom->dev->sector_size);
        
        if (!ped_geometry_read (geom, buf, 0, 1))
                return 0;
	memset (buf, 0, 512);
	return ped_geometry_write (geom, buf, 0, 1);
}
#endif /* !DISCOVER_ONLY */


static PedFileSystem*
bfs_alloc (const PedGeometry* geom)
{
	PedFileSystem*  fs;

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

	fs->type_specific = (struct BfsSpecific*) ped_malloc (
                        sizeof (struct BfsSpecific));
	if (!fs->type_specific)
		goto error_free_fs;

	fs->geom = ped_geometry_duplicate (geom);
	if (!fs->geom)
		goto error_free_type_specific;

	fs->checked = 0;
	return fs;

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


void
bfs_free (PedFileSystem* fs)
{
        ped_geometry_destroy (fs->geom);
        ped_free (fs->type_specific);
        ped_free (fs);
}


static PedFileSystem* 
bfs_open (PedGeometry *geom)
{
        PedFileSystem* fs = bfs_alloc (geom);
        
        struct bfs_sb* sb = (struct bfs_sb*) ped_malloc(sizeof(struct bfs_sb));
        struct BfsSpecific* bfs;
        uint8_t* buf;
       
        PED_ASSERT (geom      != NULL, return NULL);
        PED_ASSERT (geom->dev != NULL, return NULL);
        
        buf = ped_malloc (geom->dev->sector_size);
        
        if (!fs)
                return NULL;

        bfs = fs->type_specific;
        
        if (!ped_geometry_read (geom, buf, 0, 1))
                return NULL;
        
        memcpy (sb, buf, BFS_SECTOR_SIZE);
                        
        bfs->sb = sb;

        return fs;
}


#ifndef DISCOVER_ONLY
static struct bfs_inode* create_root_inode()
{
        struct bfs_inode* root = ped_malloc (sizeof(struct bfs_inode));

        root->i = 2UL;
        /*root->start = FIX;
        root->end = ;
        root->eof_off = ;*/
        root->attr = 2UL;
        root->mode = 512UL; /* rwxrwxrwx */
        root->uid = root->gid = 0UL;
        root->nlinks = 0UL;
        root->atime = root->ctime = 0UL;
        memset ((void*)root->reserved, 0, 32*4);
        
        return root;
}


static uint8_t* _block_alloc (int n)
{
        return ped_calloc (n * BFS_SECTOR_SIZE);
}


static void _write_inodes (PedFileSystem* fs)
{
}


/* write a BFS block - always 512 bytes */
static int _write_block (PedFileSystem* fs, uint8_t* buf, int n)
{
        /* FIXME: support for bs != 2^9 */
        return ped_geometry_write ( fs->geom, buf, n, 1 );
}


static int _write_sb (PedFileSystem* fs)
{
        uint8_t* sb = _block_alloc (1);
        
        BFS_SB(fs)->magic  = BFS_MAGIC;
        BFS_SB(fs)->sanity = BFS_PED_SANITY;
        BFS_SB(fs)->start  = BFS_SPECIFIC(fs)->data_start;
        BFS_SB(fs)->size   = BFS_SPECIFIC(fs)->size;
        
        memcpy (sb, BFS_SB(fs), sizeof(struct bfs_sb));

        return _write_block (fs, sb, 1);
}


static PedFileSystem* 
bfs_create (PedGeometry *geom, PedTimer *timer)
{
        PedFileSystem* fs = bfs_alloc (geom);
        int n_inodes = PED_MAX (BFS_PED_MIN_INODES, 16/*some sane value here*/);
        
        /* TODO: check whether geometry is big enough */
        
        fs->data_start = 1 + ped_round_up_to (n_inodes * 64, 512);
        fs->size = geom->dev->sector_size * length;
        
        ped_timer_set_state_name (timer, "Writing inodes"); 
        
       
        
        ped_timer_set_state_name (timer, "Writing super block");
        _write_sb (fs);
                
        return 0; 
}
#endif /* !DISCOVER_ONLY */


static PedFileSystemOps bfs_ops = {
	probe:		bfs_probe,
#ifndef DISCOVER_ONLY
	clobber:	bfs_clobber,
#else
	clobber:	NULL,
#endif 
	open:		bfs_open,
#ifndef DISCOVER_ONLY
	create:		bfs_create,
#else
        create:         NULL
#endif
	close:		NULL,
	check:		NULL,
	copy:		NULL,
	resize:		NULL,
	get_create_constraint:	NULL,
	get_resize_constraint:	NULL,
	get_copy_constraint:	NULL
};

static PedFileSystemType bfs_type = {
	next:	        NULL,
	ops:	        &bfs_ops,
	name:	        "bfs",
        block_sizes:    ((int[2]){512, 0})
};

void
ped_file_system_bfs_init ()
{
	ped_file_system_type_register (&bfs_type);
}

void
ped_file_system_bfs_done ()
{
	ped_file_system_type_unregister (&bfs_type);
}