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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
//! Detail module
//!
//! Contains implementations of various trait defined in the interface module.

use sunrise_libuser::fs::{DiskId, FileSystemType, PartitionId};
use alloc::boxed::Box;
use alloc::sync::Arc;
use crate::LibUserResult;
use crate::interface::storage::{PartitionStorage, IStorage};
use crate::interface::filesystem::FileSystemOperations;
use sunrise_libuser::error::{Error, FileSystemError};

use crc::{crc32, Hasher32};
use spin::Mutex;
use uuid::Uuid;

use alloc::vec::Vec;
use byteorder::{LE, ByteOrder};

pub mod driver;
mod gpt;
mod utils;

use gpt::{GPTHeader, GPTPartitionEntry};
use utils::lba_to_cls;

use driver::DRIVER_MANAGER;

/// Manage partition of a IStorage.
pub struct PartitionManager<'a> {
    /// The IStorage used.
    inner: &'a mut dyn IStorage<Error = Error>
}

/// Size of an MBR header.
const MBR_LEN: usize = 512;
/// Size of an MBR header in u64.
const MBR_LEN_U64: u64 = MBR_LEN as u64;

// TODO: Query block size from disk.
// BODY: We shouldn't be hardcoding block sizes here. We should be querying them
// BODY: from the disk.
/// Size of a block.
const BLOCK_SIZE: usize = 512;
/// Size of a block in u64.
const BLOCK_SIZE_U64: u64 = BLOCK_SIZE as u64;

impl<'a> PartitionManager<'a> {
    /// Create a new partition manager.
    pub fn new(inner: &'a mut dyn IStorage<Error = Error>) -> Self {
        PartitionManager { inner }
    }

    /// Check if the partition table is valid.
    #[allow(clippy::wrong_self_convention)]
    pub fn is_valid(&mut self) -> bool {
        let res = GPTHeader::from_storage_device(self.inner, 1);

        if let Ok(res) = res {
            return res.is_valid()
        }

        false
    }

    /// Create a protective MBR
    pub fn create_protective_mbr(&mut self) -> LibUserResult<()> {
        let mut mbr = [0x0; MBR_LEN];

        let partition_offset = 1;
        let partition_number = 1;
        let head_count = 64;
        let mut sector_count = self.inner.len()? / MBR_LEN_U64;
        if sector_count > u64::from(u32::max_value()) {
            sector_count = u64::from(u32::max_value());
        }

        let (head_number, sector_number, cylinder_number) = lba_to_cls(partition_number, head_count, sector_count);

        // Setup first fake partition.
        mbr[0x1BE] = 0x0; // not bootable

        // start CHS
        mbr[0x1BF] = head_number;
        mbr[0x1C0] = sector_number;
        mbr[0x1C1] = cylinder_number;

        // GPT protective
        mbr[0x1C2] = 0xEE;

        let (head_number, sector_number, cylinder_number) = lba_to_cls(sector_count - 1, head_count, sector_count);

        // end CHS
        mbr[0x1C3] = head_number;
        mbr[0x1C4] = sector_number;
        mbr[0x1C5] = cylinder_number;

        // finally start/end LBA.
        LE::write_u32(&mut mbr[0x1C6..0x1CA], partition_offset as u32);
        LE::write_u32(&mut mbr[0x1CA..0x1CE], sector_count as u32 - partition_offset);

        // And finally the "valid signature"
        mbr[0x1FE] = 0x55;
        mbr[0x1FF] = 0xAA;

        self.inner.write(0, &mbr)?;
        self.inner.flush()
    }

    /// Initialize a IStorage partition table.
    pub fn initialize(&mut self) -> LibUserResult<()> {
        self.create_protective_mbr()?;
        let sector_count = self.inner.len()? / MBR_LEN_U64;

        assert!(sector_count > 34, "The storage is too small to hold a GPT partition schema");

        // first setup the GPT header
        let mut primary_gpt_header = GPTHeader::default();

        // one disk id for the sake of completness
        primary_gpt_header.set_disk_guid(Uuid::parse_str("CAFECAFE-CAFE-CAFE-CAFE-CAFECAFECAFE").unwrap());
        primary_gpt_header.current_lba = 1;
        primary_gpt_header.backup_lba = sector_count - 1;
        primary_gpt_header.first_usable = 34;
        primary_gpt_header.last_usable = sector_count - 34;
        primary_gpt_header.partition_table_start = 2;

        let mut partition_table = Vec::new();

        let mut main_partition = GPTPartitionEntry::default();
        // Microsoft basic data GUID
        main_partition.set_partition_type(Uuid::parse_str("EBD0A0A2-B9E5-4433-87C0-68B6B72699C7").unwrap());

        // Some GUID selected for the sake of randomness
        main_partition.set_unique_id(Uuid::parse_str("BA3E4ADC-EB06-11E7-8AD3-9570BEC474F8").unwrap());

        // some name
        main_partition.set_name("SunriseOS System");

        // Set the start of the partition at the first LBA availaible.
        main_partition.first_lba = 34;

        // Set the last LBA just before the backup GPT
        main_partition.last_lba = sector_count - 34;

        partition_table.push(main_partition);

        // By standard, there should be at least 128 entries in the partition table.
        if partition_table.len() < 128 {
            partition_table.resize(128, GPTPartitionEntry::default());
        }

        primary_gpt_header.partition_entry_count = partition_table.len() as u32;

        let main_partition_bytes = main_partition.write();

        let mut partition_table_digest = crc32::Digest::new(crc32::IEEE);


        for (i, partition) in partition_table.iter().enumerate() {
            let raw_partition = partition.write();

            let i = (i * core::mem::size_of::<GPTPartitionEntry>()) as u64;
            self.inner.write(primary_gpt_header.partition_table_start * BLOCK_SIZE_U64 + i, &raw_partition)?;
            partition_table_digest.write(&raw_partition);
        }

        // Setup the CRC of the partition table.
        primary_gpt_header.partition_table_crc32 = partition_table_digest.sum32();

        // Finally update the CRC32
        primary_gpt_header.update_header_crc();

        // Time to write all headers now
        self.inner.write(primary_gpt_header.current_lba * BLOCK_SIZE_U64, &primary_gpt_header.write(true))?;

        // AND finally, setup and write the backup GPT
        primary_gpt_header.current_lba = sector_count - 1;
        primary_gpt_header.backup_lba = 1;
        primary_gpt_header.partition_table_start = sector_count - 33;
        primary_gpt_header.update_header_crc();
        self.inner.write(primary_gpt_header.current_lba * BLOCK_SIZE_U64, &primary_gpt_header.write(true))?;
        self.inner.write(primary_gpt_header.partition_table_start * BLOCK_SIZE_U64, &main_partition_bytes)?;

        self.inner.flush()
    }
}

/// Iterator over GPT partitions
#[derive(Debug)]
struct PartitionIterator<'a> {
    /// The IStorage used.
    inner: &'a mut dyn IStorage<Error = Error>,

    /// Partition sector start.
    partition_table_start: u64,

    /// Partition count.
    partition_entry_count: u64,

    /// Partition entry size
    partition_entry_size: u64,

    /// Current position of the iterator.
    position: u64,

    /// Stop the iterator at free entries.
    block_at_free_entry: bool
}

impl<'a> PartitionIterator<'a> {
    /// Create a new partition iterator.
    pub fn new(inner: &'a mut dyn IStorage<Error = Error>, block_at_free_entry: bool) -> LibUserResult<Self> {
        let mut res = PartitionIterator {
            inner,
            partition_table_start: 0,
            partition_entry_count: 0,
            partition_entry_size: 0,
            position: 0,
            block_at_free_entry
        };

        let partition_header = GPTHeader::from_storage_device(res.inner, 1)?;

        if !partition_header.is_valid() {
            return Err(FileSystemError::PartitionNotFound.into());
        }

        res.partition_table_start = partition_header.partition_table_start;
        res.partition_entry_count = u64::from(partition_header.partition_entry_count);
        res.partition_entry_size = u64::from(partition_header.partition_entry_size);
        Ok(res)
    }
}

impl<'a> Iterator for PartitionIterator<'a> {
    type Item = LibUserResult<GPTPartitionEntry>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.position < self.partition_entry_count {
            let mut partition_data = [0x0; core::mem::size_of::<GPTPartitionEntry>()];
            if let Err(error) = self.inner.read(self.partition_table_start * BLOCK_SIZE_U64 + self.position * self.partition_entry_size, &mut partition_data) {
                return Some(Err(error));
            }

            self.position += 1;

            let res = GPTPartitionEntry::from_bytes(partition_data);

            //If the next entry is a free entry, ignore and terminate the iterator.
            if res.partition_type.to_uuid().is_nil() {
                self.position = self.partition_entry_count;
                return None;
            }
            return Some(Ok(res))
        }
        None
    }
}

/// Entry point of the file system interface.
///
/// Allows to interract with various filesytem.
#[derive(Debug, Default, Clone)]
pub struct FileSystemProxy {

}

impl FileSystemProxy {
    /// Open a disk partition filesystem.
    /// This may fail if no compatible driver i
    pub fn open_disk_partition(&mut self, disk_id: DiskId, partition_id: PartitionId) -> LibUserResult<Arc<Mutex<Box<dyn FileSystemOperations>>>> {
        let storage = self.open_disk_storage(disk_id)?;
        let partition_option = PartitionIterator::new(storage.lock().as_mut(), true)?.nth(partition_id as usize);

        if let Some(partition) = partition_option {
            let partition = partition?;

            let partition_start = partition.first_lba * BLOCK_SIZE_U64;
            let partition_len = (partition.last_lba * BLOCK_SIZE_U64) - partition_start;

            let storage = PartitionStorage::new(storage, partition_start, partition_len);
            return DRIVER_MANAGER.lock().construct_filesystem_from_disk_partition(disk_id, partition_id, storage);
        }


        Err(FileSystemError::PartitionNotFound.into())
    }

    /// Open a disk as a block device.
    /// This may fail if no partition table is found.
    pub fn open_disk_storage(&mut self, disk_id: DiskId) -> LibUserResult<Arc<Mutex<Box<dyn IStorage<Error = Error>>>>> {
        DRIVER_MANAGER.lock().open_disk_storage(disk_id)
    }

    /// Get the count of disks availaible.
    pub fn get_disks_count(&mut self) -> LibUserResult<u32> {
        Ok(DRIVER_MANAGER.lock().get_disks_count())
    }

    /// Format a disk partition to the given filesystem type.
    pub fn format_disk_partition(&mut self, disk_id: DiskId, partition_id: PartitionId, filesytem_type: FileSystemType) -> LibUserResult<()> {
        let storage = self.open_disk_storage(disk_id)?;
        let partition_option = PartitionIterator::new(storage.lock().as_mut(), true)?.nth(partition_id as usize);

        if let Some(partition) = partition_option {
            let partition = partition?;

            let partition_start = partition.first_lba * BLOCK_SIZE_U64;
            let partition_len = (partition.last_lba * BLOCK_SIZE_U64) - partition_start;

            let storage = PartitionStorage::new(storage, partition_start, partition_len);
            return DRIVER_MANAGER.lock().format_disk_partition(storage, filesytem_type);
        }


        Err(FileSystemError::PartitionNotFound.into())
    }

    /// Initialize a disk partition table
    pub fn initialize_disk(&mut self, disk_id: DiskId) -> LibUserResult<()> {
        let storage_arc = self.open_disk_storage(disk_id)?;
        let mut storage = storage_arc.lock();
        let mut partition_manager = PartitionManager::new(storage.as_mut());
        partition_manager.initialize()
    }
}