Skip to content

Instantly share code, notes, and snippets.

@NewEraCracker
Last active March 7, 2019 20:35
Show Gist options
  • Save NewEraCracker/b83772225738cbe6efc21e6f7526eedf to your computer and use it in GitHub Desktop.
Save NewEraCracker/b83772225738cbe6efc21e6f7526eedf to your computer and use it in GitHub Desktop.
For updates please check: https://github.com/NewEraCracker/bin2dat
@REM For updates please check: https://github.com/NewEraCracker/bin2dat
@REM
@REM Creates the microcode.dat from an intel-ucode folder
@REM
@REM Date: Dec 25th 2018
@REM Author: Jorge Oliveira
@REM License: Public Domain
@REM
@REM No warranties or guarantees express or implied.
@REM Configurable variables
set CPUID_EXE=.\cpuid
set BIN2DAT_EXE=.\bin2dat
set INTEL_UC_DIR=.\intel-ucode
@REM Set to * for all, or empty for automatic detection
set UC_FILE=*
@REM Uncomment the next three lines to build the executables
@REM set GCC_EXE=cl
@REM "%GCC_EXE%" -o "%CPUID_EXE%" -O2 "%CPUID_EXE%.c"
@REM "%GCC_EXE%" -o "%BIN2DAT_EXE%" -O2 "%BIN2DAT_EXE%.c"
@REM Determine microcode file name
if _%UC_FILE%_==__ for /f "tokens=2 delims=()" %%a in ('%CPUID_EXE%') do set UC_FILE=%%a
@REM Blank the destination file
echo. 1>nul 2>microcode.dat
@REM List matching files in microcode dir
for /F "tokens=*" %%a in ('dir /b "%INTEL_UC_DIR%\%UC_FILE%"') do (
@REM Convert
"%BIN2DAT_EXE%" "%INTEL_UC_DIR%\%%a" "%INTEL_UC_DIR%\%%a.dat" 1>&2
@REM Header
echo /* %%a */
@REM Body
type "%INTEL_UC_DIR%\%%a.dat"
@REM Clean
del "%INTEL_UC_DIR%\%%a.dat"
@REM Save in microcode.dat
) >> microcode.dat
#!/bin/sh
# For updates please check: https://github.com/NewEraCracker/bin2dat
#
# Creates the microcode.dat from an intel-ucode folder
#
# Date: Dec 25th 2018
# Author: Jorge Oliveira
# License: Public Domain
#
# No warranties or guarantees express or implied.
# Configurable variables
CPUID_EXE=./cpuid
BIN2DAT_EXE=./bin2dat
INTEL_UC_DIR=./intel-ucode
# Set to '*' for all, or '' for automatic detection
UC_FILE='*'
# Uncomment the next three lines to build the executables
# GCC_EXE=gcc
# "${GCC_EXE}" -o "${CPUID_EXE}" -O2 "${CPUID_EXE}".c
# "${GCC_EXE}" -o "${BIN2DAT_EXE}" -O2 "${BIN2DAT_EXE}".c
# Determine microcode file name
if [ -z "${UC_FILE}" ]; then
UC_FILE="$("${CPUID_EXE}" | tr -d '\(\)' | awk '{print $2}')"
fi
# List matching files in microcode dir
find "${INTEL_UC_DIR}" -maxdepth 1 -type f -name "${UC_FILE}" -print | sort | while read filename; do
# Convert
"${BIN2DAT_EXE}" "${filename}" "${filename}.dat" 1>&2
# Header
printf '%s\r\n' "/* $(basename "${filename}") */"
# Body
cat "${filename}.dat"
# Clean
rm "${filename}.dat"
# Save in microcode.dat
done > microcode.dat
/**
* For updates please check: https://github.com/NewEraCracker/bin2dat
*
* Convert a binary file into dat ascii-hex format
*
* Date: Dec 25th 2018
* Author: Jorge Oliveira
* License: Public Domain
*
* No warranties or guarantees express or implied.
*/
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char * argv[])
{
// Declare variables for C89
int read, count;
FILE *in, *out;
// Check number of arguments
if (argc != 3) {
printf("Usage: %s <microcode.bin> <microcode.dat>\n", argv[0]);
return 1;
}
// Open in for reading
in = fopen(argv[1], "rb");
if (in == NULL) {
printf("ERROR: Unable to open file for read: %s\n", argv[1]);
return 1;
}
// Open out for writing
out = fopen(argv[2], "wb");
if (out == NULL) {
fclose(in); // Don't leak descriptors
printf("ERROR: Unable to open file for write: %s\n", argv[2]);
return 1;
}
// Initialize counters
read = 0;
count = 0;
do {
// Read in four byte chunks
unsigned int buffer = 0;
read = fread(&buffer, 1, sizeof(buffer), in);
// Check for EOF and invalid reads
if (!read) {
break;
} else if(read != 4) {
printf("WARN: Invalid read from: %s\n", argv[1]);
}
// Output conversion result
fprintf(out, "0x%08x,", buffer);
// Whitespace
if(++count % 4) {
fwrite(" ", 1, 1, out);
} else {
fwrite("\n", 1, 1, out);
}
} while (read);
// Check for unaligned reads
if(count % 4) {
printf("WARN: Unaligned read from: %s\n", argv[1]);
fwrite("\n", 1, 1, out);
}
// Flush output
fflush(out);
// We're done, close descriptors
fclose(out);
fclose(in);
return 0;
}
/**
* For updates please check: https://github.com/NewEraCracker/bin2dat
*
* Discover the cpuid of an Intel processor
*
* Date: Dec 25th 2018
* Author: Jorge Oliveira
* License: Public Domain
*
* No warranties or guarantees express or implied.
*/
#include <stdio.h>
#include <stdlib.h>
unsigned int cpuid(unsigned int type)
{
unsigned int id = type;
#ifndef _MSC_VER
__asm__("cpuid" : "=a" (id) : "a" (id));
#else
__asm {
mov eax, id
cpuid
mov id, eax
}
#endif
return id;
}
int main(int argc, char * argv[])
{
unsigned int id = cpuid(1);
unsigned char step = (id) & 0x0000000f;
unsigned char model = (((id >> 16) & 0x000000ff) << 4) | ((id >> 4) & 0x0000000f);
unsigned char family = (((id >> 20) & 0x000000ff) << 4) | ((id >> 8) & 0x0000000f);
printf("%08x (%02x-%02x-%02x)", id, family, model, step);
fflush(stdout);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment