blob: 4890e120fb7a991a71a726ff2ed9b592a70496cc [file] [log] [blame]
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001/*
2 rootdir.c (09.11.10)
3 Root directory creation code.
4
bigbiff bigbiffca829c42013-01-28 08:14:25 -05005 Copyright (C) 2011-2013 Andrew Nayenko
bigbiff bigbiff9c754052013-01-09 09:09:08 -05006
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
19*/
20
21#include <string.h>
22#include "rootdir.h"
23#include "uct.h"
24#include "cbm.h"
25#include "uctc.h"
26
27static off64_t rootdir_alignment(void)
28{
29 return get_cluster_size();
30}
31
32static off64_t rootdir_size(void)
33{
34 return get_cluster_size();
35}
36
37static void init_label_entry(struct exfat_entry_label* label_entry)
38{
39 memset(label_entry, 0, sizeof(struct exfat_entry_label));
40 label_entry->type = EXFAT_ENTRY_LABEL ^ EXFAT_ENTRY_VALID;
41
42 if (utf16_length(get_volume_label()) == 0)
43 return;
44
45 memcpy(label_entry->name, get_volume_label(),
46 EXFAT_ENAME_MAX * sizeof(le16_t));
47 label_entry->length = utf16_length(get_volume_label());
48 label_entry->type |= EXFAT_ENTRY_VALID;
49}
50
51static void init_bitmap_entry(struct exfat_entry_bitmap* bitmap_entry)
52{
53 memset(bitmap_entry, 0, sizeof(struct exfat_entry_bitmap));
54 bitmap_entry->type = EXFAT_ENTRY_BITMAP;
55 bitmap_entry->start_cluster = cpu_to_le32(EXFAT_FIRST_DATA_CLUSTER);
56 bitmap_entry->size = cpu_to_le64(cbm.get_size());
57}
58
59static void init_upcase_entry(struct exfat_entry_upcase* upcase_entry)
60{
61 size_t i;
62 uint32_t sum = 0;
63
64 for (i = 0; i < sizeof(upcase_table); i++)
65 sum = ((sum << 31) | (sum >> 1)) + upcase_table[i];
66
67 memset(upcase_entry, 0, sizeof(struct exfat_entry_upcase));
68 upcase_entry->type = EXFAT_ENTRY_UPCASE;
69 upcase_entry->checksum = cpu_to_le32(sum);
70 upcase_entry->start_cluster = cpu_to_le32(
71 (get_position(&uct) - get_position(&cbm)) / get_cluster_size() +
72 EXFAT_FIRST_DATA_CLUSTER);
73 upcase_entry->size = cpu_to_le64(sizeof(upcase_table));
74}
75
76static int rootdir_write(struct exfat_dev* dev)
77{
78 struct exfat_entry_label label_entry;
79 struct exfat_entry_bitmap bitmap_entry;
80 struct exfat_entry_upcase upcase_entry;
81
82 init_label_entry(&label_entry);
83 init_bitmap_entry(&bitmap_entry);
84 init_upcase_entry(&upcase_entry);
85
86 if (exfat_write(dev, &label_entry, sizeof(struct exfat_entry)) < 0)
87 return 1;
88 if (exfat_write(dev, &bitmap_entry, sizeof(struct exfat_entry)) < 0)
89 return 1;
90 if (exfat_write(dev, &upcase_entry, sizeof(struct exfat_entry)) < 0)
91 return 1;
92 return 0;
93}
94
95const struct fs_object rootdir =
96{
97 .get_alignment = rootdir_alignment,
98 .get_size = rootdir_size,
99 .write = rootdir_write,
100};