/*++

Copyright (c) 2015 Minoca Corp. All Rights Reserved

Module Name:

    common.txt

Abstract:

    This module adds common definitions to the setup interpreter.

Author:

    Evan Green 19-Oct-2015

Environment:

    Setup

--*/

//
// Constants
//

TRUE = 1;
FALSE = 0;
KILOBYTE = 1024;
MEGABYTE = KILOBYTE * 1024;
GIGABYTE = MEGABYTE * 1024;
TERABYTE = GIGABYTE * 1024;
PETABYTE = TERABYTE * 1024;
EXABYTE = PETABYTE * 1024;

EFI_DEFAULT_APP_X86 = "BOOTIA32.EFI";
EFI_DEFAULT_APP_ARM = "BOOTARM.EFI";

PARTITION_FORMAT_NONE = 1;
PARTITION_FORMAT_MBR = 2;
PARTITION_FORMAT_GPT = 3;

PARTITION_TYPE_EFI_SYSTEM =
    "\x28\x73\x2A\xC1\x1F\xF8\xD2\x11\xBA\x4B\x00\xA0\xC9\x3E\xC9\x3B";

PARTITION_TYPE_MINOCA =
    "\xCC\x07\xA3\xCE\xBD\x78\x40\x6E\x81\x62\x60\x20\xAF\xB8\x8D\x17";

PARTITION_ID_DOS_FAT12 = 0x01;
PARTITION_ID_PRIMARY_FAT16 = 0x04;
PARTITION_ID_FAT32 = 0x0B;
PARTITION_ID_FAT32_LBA = 0x0C;
PARTITION_ID_EFI_GPT = 0xEE;
PARTITION_ID_MINOCA = 0x6B;

//
// Path configuration
//

//
// All of the source files are located inside of a directory rather than
// directly at the root because some file systems have a limited number of
// entries in the root directory (FAT12/16).
//

SourceDir = "bin/";
SystemRoot = "minoca/";
SystemDirName = "system/";
SystemDir = SystemRoot + SystemDirName;
SystemConfigDir = SystemRoot + "config/";
DriversDir = SystemRoot + "drivers/";
EfiBootDir = "EFI/BOOT/";
EfiMinocaDir = "EFI/MINOCA/";
LoaderPathEfi = SystemDirName + "loadefi";
LoaderPathPcat = SystemDirName + "loader";
KernelPath = SystemDirName + "kernel";

//
// File lists
//

DriverFiles = [
    "acpi.drv",
    "ehci.drv",
    "fat.drv",
    "net80211.drv",
    "netcore.drv",
    "null.drv",
    "onering.drv",
    "part.drv",
    "pci.drv",
    "rtlw81xx.drv",
    "rtlw8188eufw.bin",
    "rtlw8188cufwUMC.bin",
    "rtlw8192cufw.bin",
    "ser16550.drv",
    "sd.drv",
    "smsc95xx.drv",
    "special.drv",
    "usbcomp.drv",
    "usbcore.drv",
    "usbhub.drv",
    "usbkbd.drv",
    "usbmass.drv",
    "usrinput.drv",
    "videocon.drv",
];

DriverFilesX86 = [
    "ata.drv",
    "atl1c.drv",
    "dwceth.drv",
    "e100.drv",
    "i8042.drv",
    "rtl81xx.drv",
    "uhci.drv",
];

DriverFilesArm = [
    "dma.drv",
    "gpio.drv",
    "spb.drv",
];

BootDrivers = [
    "acpi.drv",
    "fat.drv",
    "null.drv",
    "part.drv",
    "special.drv",
    "videocon.drv",
];

BootDriversX86 = [
    "ata.drv",
    "pci.drv",
    "ehci.drv",
    "usbcomp.drv",
    "usbhub.drv",
    "usbmass.drv",
    "sd.drv",
];

SystemConfigFiles = [
    "dev2drv.set",
    "devmap.set",
    "init.set",
    "init.sh",
    "tzdata",
    "tzdflt",
];

SystemFiles = [
    "kernel",
    "loadefi",
    "bootmefi.efi",
    "libminocaos.so.1",
];

SystemFilesX86Pcat = [
    "mbr.bin",
    "fatboot.bin",
    "bootman.bin",
    "loader",
];

//
// Copy commands
//

DriversCopy = {
    "Destination": DriversDir,
    "Source": SourceDir,
    "SourceVolume": 0,
    "Files": DriverFiles
};

SystemCopy = {
    "Destination": SystemDir,
    "Source": SourceDir,
    "SourceVolume": 0,
    "Files": SystemFiles
};

SystemConfigCopy = {
    "Destination": SystemConfigDir,
    "Source": SourceDir,
    "SourceVolume": 0,
    "Files": SystemConfigFiles
};

BootmefiCopy = {
    "Destination": EfiBootDir,
    "Source": SourceDir + "bootmefi.efi",
    "SourceVolume": 0,
    "Update": TRUE,
};

BootmefiBackupCopy = {
    "Destination": EfiBootDir + "bootmefi.efi",
    "Source": SourceDir + "bootmefi.efi",
    "SourceVolume": 0,
};

UserAppsCopy = {
    "Destination": "/apps/",
    "Source": SourceDir + "apps/",
    "SourceVolume": 0,
    "Optional": TRUE
};

UserSkelCopy = {
    "Destination": "/apps/",
    "Source": SourceDir + "skel/",
    "SourceVolume": 0,
};

TotalBootCopy = [BootmefiCopy, BootmefiBackupCopy];
TotalCopy = [
    DriversCopy,
    SystemCopy,
    SystemConfigCopy,
    UserSkelCopy,
    UserAppsCopy
];

//
// Partition descriptions
//

BootPartition = {
    "Index": 0,
    "Size": 10 * MEGABYTE,
    "PartitionType": PARTITION_TYPE_EFI_SYSTEM,
    "MbrType": PARTITION_ID_PRIMARY_FAT16,
    "Files": TotalBootCopy,
    "Attributes": 0,
    "Alignment": 4 * KILOBYTE,
    "Flags": {
        "Boot": TRUE,
        "System": FALSE,
        "CompatibilityMode": TRUE,
        "WriteVbrLba": FALSE,
        "MergeVbr": FALSE,
    },
};

SystemPartition = {
    "Index": 1,
    "Size": -1,
    "PartitionType": PARTITION_TYPE_MINOCA,
    "MbrType": PARTITION_ID_MINOCA,
    "Files": TotalCopy,
    "Attributes": 0,
    "Alignment": 4 * KILOBYTE,
    "Flags": {
        "Boot": FALSE,
        "System": TRUE,
        "CompatibilityMode": FALSE,
        "WriteVbrLba": FALSE,
        "MergeVbr": FALSE,
    },
};

Partitions = [BootPartition, SystemPartition];

DiskData = {
    "Format": PARTITION_FORMAT_GPT,
    "Partitions": Partitions
};

//
// Boot entry settings
//

BootEntry = {
    "Name": "Minoca OS",
    "LoaderArguments": "",
    "KernelArguments": "",
    "LoaderPath": LoaderPathEfi,
    "KernelPath": KernelPath,
    "SystemPath": SystemRoot,
    "Flags": {
        "Debug": FALSE,
        "BootDebug": FALSE,
    },

    "DebugDevice": 0,
};

BootConfiguration = {
    "Timeout": 0,
    "DataPath": EfiMinocaDir + "bootconf",
    "BootEntries": [BootEntry]
};

//
// Driver database configuration
//

DriverDb = {
    "BootDrivers": BootDrivers,
    "BootDriversPath": SystemConfigDir + "bootdrv.set"
};

//
// Final settings compilation
//

Settings = {
    "Disk": DiskData,
    "BootConfiguration": BootConfiguration,
    "DriverDb": DriverDb
};

