| 1939 | } |
| 1940 | |
| 1941 | static void inject_region(const char *filename, char *image, int size, |
| 1942 | unsigned int region_type, const char *region_fname) |
| 1943 | { |
| 1944 | struct frba *frba = find_frba(image, size); |
| 1945 | if (!frba) |
| 1946 | exit(EXIT_FAILURE); |
| 1947 | |
| 1948 | struct region region = get_region(frba, region_type); |
| 1949 | if (region.size <= 0xfff) { |
| 1950 | fprintf(stderr, "Region %s is disabled in target. Not injecting.\n", |
| 1951 | region_name(region_type)); |
| 1952 | exit(EXIT_FAILURE); |
| 1953 | } |
| 1954 | |
| 1955 | int region_fd = open(region_fname, O_RDONLY | O_BINARY); |
| 1956 | if (region_fd == -1) { |
| 1957 | perror("Could not open file"); |
| 1958 | exit(EXIT_FAILURE); |
| 1959 | } |
| 1960 | struct stat buf; |
| 1961 | if (fstat(region_fd, &buf) == -1) { |
| 1962 | perror("Could not stat file"); |
| 1963 | exit(EXIT_FAILURE); |
| 1964 | } |
| 1965 | int region_size = buf.st_size; |
| 1966 | |
| 1967 | printf("File %s is %d bytes\n", region_fname, region_size); |
| 1968 | |
| 1969 | if (region_size > region.size) { |
| 1970 | fprintf(stderr, "Region %s is %d(0x%x) bytes. File is %d(0x%x)" |
| 1971 | " bytes. Not injecting.\n", |
| 1972 | region_name(region_type), region.size, |
| 1973 | region.size, region_size, region_size); |
| 1974 | exit(EXIT_FAILURE); |
| 1975 | } |
| 1976 | |
| 1977 | int offset = 0; |
| 1978 | if ((region_type == 1) && (region_size < region.size)) { |
| 1979 | fprintf(stderr, "Region %s is %d(0x%x) bytes. File is %d(0x%x)" |
| 1980 | " bytes. Padding before injecting.\n", |
| 1981 | region_name(region_type), region.size, |
| 1982 | region.size, region_size, region_size); |
| 1983 | offset = region.size - region_size; |
| 1984 | memset(image + region.base, 0xff, offset); |
| 1985 | } |
| 1986 | |
| 1987 | if (size < region.base + offset + region_size) { |
| 1988 | fprintf(stderr, "Output file is too small. (%d < %d)\n", |
| 1989 | size, region.base + offset + region_size); |
| 1990 | exit(EXIT_FAILURE); |
| 1991 | } |
| 1992 | |
| 1993 | if (read(region_fd, image + region.base + offset, region_size) != region_size) { |
| 1994 | perror("Could not read file"); |
| 1995 | exit(EXIT_FAILURE); |
| 1996 | } |
| 1997 | |
| 1998 | close(region_fd); |
no test coverage detected