summaryrefslogtreecommitdiffhomepage
path: root/public/frameworks/DiskImage.fwrk/src/DiskImage+EPM.cc
blob: f9880560b43155c2cad64f65b1d54a7e77c69abf (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
// Copyright 2024-2025, Amlal El Mahrouss (amlal@nekernel.org)
// Licensed under the Apache License, Version 2.0 (see LICENSE file)
// Official repository: https://github.com/nekernel-org/nekernel

#include <DiskImage.fwrk/headers/DiskImage.h>

#include <FSKit/NeFS.h>
#include <FirmwareKit/EPM.h>

/// @brief Format a disk under the EPM scheme.
/// @param img disk image structure.
/// @return Status code upon completion.
SInt32 DI::DIFormatPartitionEPM(struct DI_DISK_IMAGE& img) {
  if (!img.sector_sz || (img.sector_sz % 512 != 0)) return kDIFailureStatus;
  if (!img.fs_version) return kDIFailureStatus;
  if (*img.out_name == 0 || *img.disk_name == 0) return kDIFailureStatus;

  struct ::EPM_PART_BLOCK block {};

  block.NumBlocks = img.block_cnt;
  block.SectorSz  = img.sector_sz;
  block.Version   = kEPMRevisionBcd;
  block.LbaStart  = sizeof(struct ::EPM_PART_BLOCK);
  block.LbaEnd    = img.disk_sz - block.LbaStart;
  block.FsVersion = img.fs_version;

  ::MmCopyMemory(block.Name, (VoidPtr) img.disk_name, ::MmStrLen(img.disk_name));

  ::MmCopyMemory(block.Magic, (VoidPtr) kEPMMagic, ::MmStrLen(kEPMMagic));

  IORef handle = IoOpenFile(img.out_name, nullptr);

  if (!handle) return kDIFailureStatus;

  ::IoWriteFile(handle, (Char*) &block, sizeof(struct ::EPM_PART_BLOCK));

  ::IoCloseFile(handle);

  handle = nullptr;

  return kDISuccessStatus;
}