/*
 * Copyright (c) 2023 Jonas 'Sortie' Termansen.
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 * fat.c
 * File Allocation Table filesystem.
 */

#include <stdbool.h>
#include <stddef.h>
#include <string.h>

#include <mount/blockdevice.h>
#include <mount/fat.h>
#include <mount/filesystem.h>
#include <mount/partition.h>
#include <mount/uuid.h>

#include "util.h"

static size_t fat_probe_amount(struct blockdevice* bdev)
{
	(void) bdev;
	return 512;
}

static bool fat_probe(struct blockdevice* bdev,
                      const unsigned char* leading,
                      size_t amount)
{
	(void) leading;
	(void) amount;
	// TODO: Relax restriction that this must be a partition? At least for non-EFI.
	struct partition* p = bdev->p;
	// TODO: Test for a space padded FAT32 at 0x52 + 8 bytes.
	if ( p && p->table_type == PARTITION_TABLE_TYPE_GPT )
	{
		unsigned char bdp_uuid[16];
		uuid_from_string(bdp_uuid, BDP_GPT_TYPE_UUID);
		unsigned char esp_uuid[16];
		uuid_from_string(esp_uuid, ESP_GPT_TYPE_UUID);
		// TODO: Additional probing is needed to detect FAT vs NTFS.
		return memcmp(p->gpt_type_guid, bdp_uuid, 16) == 0 ||
		       memcmp(p->gpt_type_guid, esp_uuid, 16) == 0;
	}
	else if ( p && p->table_type == PARTITION_TABLE_TYPE_MBR )
		return p->mbr_system_id == 0x01 ||
		       p->mbr_system_id == 0x04 ||
		       p->mbr_system_id == 0x06 ||
		       p->mbr_system_id == 0x04 ||
		       p->mbr_system_id == 0x0C ||
		       p->mbr_system_id == 0x0E ||
		       p->mbr_system_id == 0xEF;
	return false;
}

static bool fat_is_esp(struct blockdevice* bdev)
{
	struct partition* p = bdev->p;
	if ( p->table_type == PARTITION_TABLE_TYPE_GPT )
	{
		unsigned char esp_uuid[16];
		uuid_from_string(esp_uuid, ESP_GPT_TYPE_UUID);
		return memcmp(p->gpt_type_guid, esp_uuid, 16) == 0;
	}
	else if ( p->table_type == PARTITION_TABLE_TYPE_MBR )
		return p->mbr_system_id == 0xEF;
	return false;
}

static void fat_release(struct filesystem* fs)
{
	if ( !fs )
		return;
	free(fs);
}

static enum filesystem_error fat_inspect(struct filesystem** fs_ptr,
                                         struct blockdevice* bdev)
{
	*fs_ptr = NULL;
	struct filesystem* fs = CALLOC_TYPE(struct filesystem);
	if ( !fs )
		return FILESYSTEM_ERROR_ERRNO;
	fs->bdev = bdev;
	fs->handler = &fat_handler;
	fs->handler_private = NULL;
	unsigned char vbr[512];
	if ( blockdevice_preadall(bdev, vbr, sizeof(vbr), 0) != sizeof(vbr) )
		return fat_release(fs), FILESYSTEM_ERROR_ERRNO;
	fs->fstype_name = fat_is_esp(fs->bdev) ? "efi": "fat";
	fs->fsck = "fsck.fat";
	fs->driver = "fatfs";
	fs->flags |= FILESYSTEM_FLAG_UUID;
	// Use the serial number + label as the UUID.
	// TODO: The location varies between FAT12/16 and FAT32. This is the wrong
	// way to detect the FAT type.
	if ( vbr[82 + 3] == '3' && vbr[82 + 4] == '2' )
		memcpy(fs->uuid, vbr + 67, 4 + 11);
	else
		memcpy(fs->uuid, vbr + 39, 4 + 11);
	return *fs_ptr = fs, FILESYSTEM_ERROR_NONE;
}

const struct filesystem_handler fat_handler =
{
	.handler_name = "fat",
	.probe_amount = fat_probe_amount,
	.probe = fat_probe,
	.inspect = fat_inspect,
	.release = fat_release,
};
