MT8173 PCM header introduction
authorPaul Kocialkowski <contact@paulk.fr>
Fri, 24 Feb 2017 19:22:27 +0000 (20:22 +0100)
committerPaul Kocialkowski <contact@paulk.fr>
Fri, 24 Feb 2017 19:22:27 +0000 (20:22 +0100)
This generates headers from binaries in the ARM Trusted Firmware style.

Signed-off-by: Paul Kocialkowski <contact@paulk.fr>
Makefile
pcm-header/Makefile [new file with mode: 0644]
pcm-header/pcm-header.c [new file with mode: 0644]

index 6f570e9..6604c8d 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -13,7 +13,7 @@
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-PROJECTS = pcm-analysis pcm-assembler pcm-asm pcm-disasm
+PROJECTS = pcm-analysis pcm-assembler pcm-asm pcm-disasm pcm-header
 
 all:
        @$(foreach project,$(PROJECTS),make -C "$(project)" ;)
diff --git a/pcm-header/Makefile b/pcm-header/Makefile
new file mode 100644 (file)
index 0000000..f8c750e
--- /dev/null
@@ -0,0 +1,83 @@
+# Copyright (C) 2016-2017 Paul Kocialkowski <contact@paulk.fr>
+#
+# This program 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.
+#
+# This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
+
+# Tools
+
+CC = gcc
+
+# Project
+
+NAME = pcm-header
+
+# Directories
+
+BUILD = build
+OUTPUT = .
+
+# Sources
+
+SOURCES = pcm-header.c
+OBJECTS = $(SOURCES:.c=.o)
+DEPS = $(SOURCES:.c=.d)
+
+# Compiler
+
+INCLUDES =
+
+CFLAGS = $(foreach include,$(INCLUDES),-I$(include))
+LDFLAGS =
+
+# Produced files
+
+BUILD_OBJECTS = $(addprefix $(BUILD)/,$(OBJECTS))
+BUILD_DEPS = $(addprefix $(BUILD)/,$(DEPS))
+BUILD_BINARY = $(BUILD)/$(NAME)
+BUILD_DIRS = $(sort $(dir $(BUILD_BINARY) $(BUILD_OBJECTS)))
+
+OUTPUT_BINARY = $(OUTPUT)/$(NAME)
+OUTPUT_DIRS = $(sort $(dir $(OUTPUT_BINARY)))
+
+all: $(OUTPUT_BINARY)
+
+$(BUILD_DIRS):
+       @mkdir -p $@
+
+$(BUILD_OBJECTS): $(BUILD)/%.o: %.c | $(BUILD_DIRS)
+       @echo " CC     $<"
+       @$(CC) $(CFLAGS) -MMD -MF $(BUILD)/$*.d -c $< -o $@
+
+$(BUILD_BINARY): $(BUILD_OBJECTS)
+       @echo " LINK   $@"
+       @$(CC) $(CFLAGS) -o $@ $(BUILD_OBJECTS) $(LDFLAGS)
+
+$(OUTPUT_DIRS):
+       @mkdir -p $@
+
+$(OUTPUT_BINARY): $(BUILD_BINARY) | $(OUTPUT_DIRS)
+       @echo " BINARY $@"
+       @cp $< $@
+
+.PHONY: clean
+clean:
+       @echo " CLEAN"
+       @rm -rf $(foreach object,$(basename $(BUILD_OBJECTS)),$(object)*) $(basename $(BUILD_BINARY))*
+       @rm -rf $(OUTPUT_BINARY)
+
+.PHONY: distclean
+distclean: clean
+       @echo " DISTCLEAN"
+       @rm -rf $(BUILD)
+
+-include $(BUILD_DEPS)
diff --git a/pcm-header/pcm-header.c b/pcm-header/pcm-header.c
new file mode 100644 (file)
index 0000000..5954f13
--- /dev/null
@@ -0,0 +1,123 @@
+/*
+ * Copyright (C) 2016-2017 Paul Kocialkowski <contact@paulk.fr>
+ *
+ * This program 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.
+ *
+ * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+#define WORDS_PER_LINE                                         6
+
+int firmware_values(char *file, unsigned int **values)
+{
+       struct stat stat_data;
+       size_t size;
+       int fd = -1;
+       int rc;
+
+       if (file == NULL || values == NULL)
+               return -1;
+
+       rc = stat(file, &stat_data);
+       if (rc < 0) {
+               fprintf(stderr, "Invalid file: %s\n", file);
+               goto error;
+       }
+
+       size = stat_data.st_size;
+       *values = malloc(size);
+
+       fd = open(file, O_RDONLY, 0644);
+       if (fd < 0) {
+               fprintf(stderr, "Unable to open file: %s\n", file);
+               goto error;
+       }
+
+       rc = read(fd, *values, size);
+       if (rc <= 0) {
+               fprintf(stderr, "Unable to read file: %s\n", file);
+               goto error;
+       }
+
+       rc = size / sizeof(unsigned int);
+       goto complete;
+
+error:
+       rc = -1;
+
+       if (*values != NULL)
+               free(*values);
+
+complete:
+       if (fd >= 0)
+               close(fd);
+
+       return rc;
+}
+
+int main(int argc, char *argv[])
+{
+       unsigned int *values = NULL;
+       unsigned int count;
+       int fd = -1;
+       int rc;
+       int i;
+
+       if (argc < 3 || argv[1] == NULL || argv[2] == NULL)
+               return 1;
+
+       rc = firmware_values(argv[1], &values);
+       if (rc < 0 || values == NULL)
+               goto error;
+
+       count = rc;
+
+       fd = open(argv[2], O_WRONLY | O_TRUNC | O_CREAT, 0644);
+       if (fd < 0) {
+               fprintf(stderr, "Unable to open output file\n");
+       }
+
+       for (i = 0; i < count; i++) {
+               if (i % WORDS_PER_LINE == 0)
+                       dprintf(fd, "\t");
+
+               dprintf(fd, "0x%08x,", values[i]);
+
+               if (i % WORDS_PER_LINE == (WORDS_PER_LINE - 1))
+                       dprintf(fd, "\n");
+               else
+                       dprintf(fd, " ");
+       }
+
+       rc = 0;
+       goto complete;
+
+error:
+       rc = 1;
+
+complete:
+       if (fd >= 0)
+               close(fd);
+
+       if (values != NULL)
+               free(values);
+
+       return rc;
+}