ntloader/utils/fsuuid.c
2025-02-11 20:44:04 +09:00

143 lines
3.9 KiB
C

/*
* ntloader -- Microsoft Windows NT6+ loader
* Copyright (C) 2025 A1ive.
*
* ntloader is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published
* by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* ntloader is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ntloader. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <ctype.h>
#ifdef _WIN32
#include <windows.h>
#endif
#include "fsuuid.h"
static int fsuuid_help (const char *prog)
{
printf ("Usage:\n"
"%s VOLUME\n"
#ifdef _WIN32
"-l List all volumes\n"
#endif
, prog);
return 0;
}
#ifdef _WIN32
static int fsuuid_list (void)
{
BOOL rc;
HANDLE search;
WCHAR volume[MAX_PATH];
for (rc = TRUE, search = FindFirstVolumeW (volume, MAX_PATH);
rc && search != INVALID_HANDLE_VALUE;
rc = FindNextVolumeW (search, volume, MAX_PATH))
{
/* Removing trailing backslash */
size_t len = wcslen (volume);
if (len >= 1 && volume[len - 1] == L'\\')
volume[len - 1] = L'\0';
printf ("%ls\n", volume);
}
if (search != INVALID_HANDLE_VALUE)
FindVolumeClose (search);
return 0;
}
#endif
static void fsuuid_get (const char *volume)
{
char uuid[17] = "";
union volume_boot_record vbr;
FILE *fd = fopen (volume, "rb");
if (fd == NULL)
{
fprintf (stderr, "Could not open %s.\n", volume);
return;
}
if (fread (&vbr, sizeof (vbr), 1, fd) != 1)
{
fprintf (stderr, "Could not read %s.\n", volume);
fclose (fd);
return;
}
if (memcmp (vbr.exfat.oem_name, "EXFAT", 5) == 0)
{
snprintf (uuid, 17, "%04x-%04x",
(uint16_t) (vbr.exfat.num_serial >> 16),
(uint16_t) vbr.exfat.num_serial);
}
else if (memcmp (vbr.ntfs.oem_name, "NTFS", 4) == 0)
{
snprintf (uuid, 17, "%016llx", (unsigned long long) vbr.ntfs.num_serial);
}
else if (memcmp (vbr.fat.version.fat12_or_fat16.fstype, "FAT12", 5) == 0)
{
snprintf (uuid, 17, "%04x-%04x",
(uint16_t) (vbr.fat.version.fat12_or_fat16.num_serial >> 16),
(uint16_t) vbr.fat.version.fat12_or_fat16.num_serial);
}
else if (memcmp (vbr.fat.version.fat12_or_fat16.fstype, "FAT16", 5) == 0)
{
snprintf (uuid, 17, "%04x-%04x",
(uint16_t) (vbr.fat.version.fat12_or_fat16.num_serial >> 16),
(uint16_t) vbr.fat.version.fat12_or_fat16.num_serial);
}
else if (memcmp (vbr.fat.version.fat32.fstype, "FAT32", 5) == 0)
{
snprintf (uuid, 17, "%04x-%04x",
(uint16_t) (vbr.fat.version.fat32.num_serial >> 16),
(uint16_t) vbr.fat.version.fat32.num_serial);
}
if (uuid[0])
printf ("%s\n", uuid);
else
fprintf (stderr, "Unsupported filesystem.\n");
fclose (fd);
}
int main (int argc, char *argv[])
{
int i;
if (argc <= 1)
return fsuuid_help (argv[0]);
#ifdef _WIN32
if (argc == 2 && strcasecmp (argv[1], "-l") == 0)
return fsuuid_list ();
#endif
for (i = 1; i < argc; i++)
{
char *volume = argv[i];
#ifdef _WIN32
char drive_letter[] = "\\\\.\\c:";
if (isalpha (argv[i][0])
&& (argv[i][1] == ':' || argv[i][1] == '\0')
&& strlen (argv[i]) <= 3 /* c:\ */)
{
drive_letter[4] = argv[i][0];
volume = drive_letter;
}
#endif
fsuuid_get (volume);
}
return 0;
}