1 //===-- Main entry into the loader interface ------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file opens a device image passed on the command line and passes it to 10 // one of the loader implementations for launch. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "Loader.h" 15 16 #include <cstdio> 17 #include <cstdlib> 18 19 int main(int argc, char **argv) { 20 if (argc < 2) { 21 printf("USAGE: ./loader <device_image> <args>, ...\n"); 22 return EXIT_SUCCESS; 23 } 24 25 // TODO: We should perform some validation on the file. 26 FILE *file = fopen(argv[1], "r"); 27 28 if (!file) { 29 fprintf(stderr, "Failed to open image file %s\n", argv[1]); 30 return EXIT_FAILURE; 31 } 32 33 fseek(file, 0, SEEK_END); 34 const auto size = ftell(file); 35 fseek(file, 0, SEEK_SET); 36 37 void *image = malloc(size * sizeof(char)); 38 fread(image, sizeof(char), size, file); 39 fclose(file); 40 41 // Drop the loader from the program arguments. 42 int ret = load(argc - 1, &argv[1], image, size); 43 44 free(image); 45 return ret; 46 } 47