remove trailing whitespace
[coreboot.git] / util / cbfstool / cbfs-mkstage.c
index 693d7cc0990e9725bb67965e02dc3d4537d0bb96..6b80a37dc521a821a84d1382e34f6080d1fa214d 100644 (file)
@@ -36,10 +36,12 @@ unsigned int idemp(unsigned int x)
        return x;
 }
 
-unsigned int swap32(unsigned int x)
+/* This is a wrapper around the swab32() macro to make it
+ * usable for the current implementation of parse_elf_to_stage()
+ */
+static unsigned int swap32(unsigned int x)
 {
-       return ((x >> 24) | ((x >> 8) & 0xff00) | ((x << 8) & 0xff0000) |
-               (x << 24));
+       return swab32(x);
 }
 
 unsigned int (*elf32_to_native) (unsigned int) = idemp;
@@ -59,7 +61,6 @@ int parse_elf_to_stage(unsigned char *input, unsigned char **output,
        unsigned int data_start, data_end, mem_end;
 
        int elf_bigendian = 0;
-       int host_bigendian = 0;
 
        comp_func_ptr compress = compression_function(algo);
        if (!compress)
@@ -73,11 +74,6 @@ int parse_elf_to_stage(unsigned char *input, unsigned char **output,
        if (ehdr->e_ident[EI_DATA] == ELFDATA2MSB) {
                elf_bigendian = 1;
        }
-       char test[4] = "1234";
-       uint32_t inttest = *(uint32_t *) test;
-       if (inttest == 0x31323334) {
-               host_bigendian = 1;
-       }
        if (elf_bigendian != host_bigendian) {
                elf32_to_native = swap32;
        }
@@ -122,6 +118,15 @@ int parse_elf_to_stage(unsigned char *input, unsigned char **output,
                        mem_end = mend;
        }
 
+       if (data_start < *location) {
+               data_start = *location;
+       }
+
+       if (data_end <= data_start) {
+               fprintf(stderr, "E: data ends before it starts. Make sure the ELF file is correct and resides in ROM space.\n");
+               exit(1);
+       }
+
        /* allocate an intermediate buffer for the data */
        buffer = calloc(data_end - data_start, 1);
 
@@ -133,6 +138,7 @@ int parse_elf_to_stage(unsigned char *input, unsigned char **output,
        /* Copy the file data into the buffer */
 
        for (i = 0; i < headers; i++) {
+               unsigned int l_start, l_offset = 0;
 
                if (elf32_to_native(phdr[i].p_type) != PT_LOAD)
                        continue;
@@ -140,9 +146,15 @@ int parse_elf_to_stage(unsigned char *input, unsigned char **output,
                if (elf32_to_native(phdr[i].p_memsz) == 0)
                        continue;
 
-               memcpy(buffer + (elf32_to_native(phdr[i].p_paddr) - data_start),
-                      &header[elf32_to_native(phdr[i].p_offset)],
-                      elf32_to_native(phdr[i].p_filesz));
+               l_start = elf32_to_native(phdr[i].p_paddr);
+               if (l_start < *location) {
+                       l_offset = *location - l_start;
+                       l_start = *location;
+               }
+
+               memcpy(buffer + (l_start - data_start),
+                      &header[elf32_to_native(phdr[i].p_offset)+l_offset],
+                      elf32_to_native(phdr[i].p_filesz)-l_offset);
        }
 
        /* Now make the output buffer */
@@ -155,10 +167,10 @@ int parse_elf_to_stage(unsigned char *input, unsigned char **output,
 
        stage = (struct cbfs_stage *)out;
 
-       stage->load = data_start;
+       stage->load = data_start; /* FIXME: htonll */
        stage->memlen = mem_end - data_start;
        stage->compression = algo;
-       stage->entry = ehdr->e_entry;
+       stage->entry = ehdr->e_entry; /* FIXME: htonll */
 
        compress(buffer, data_end - data_start,
                 (char *)(out + sizeof(struct cbfs_stage)), (int *)&stage->len);