openocd: fix SPDX tag format for files .c
[fw/openocd] / src / target / image.c
index 8f72329bdb2d8d40e7c35a8d3de249bb6ff85c67..f8de7a23e3bb641e8d9f8930d9fd813c62813ffb 100644 (file)
@@ -1,3 +1,5 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+
 /***************************************************************************
  *   Copyright (C) 2007 by Dominic Rath                                    *
  *   Dominic.Rath@gmx.de                                                   *
  *                                                                         *
  *   Copyright (C) 2018 by Advantest                                       *
  *   florian.meister@advantest.com                                         *
- *                                                                         *
- *   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 2 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/>. *
  ***************************************************************************/
 
 #ifdef HAVE_CONFIG_H
@@ -123,7 +112,7 @@ static int identify_image_type(struct image *image, const char *type_string, con
 }
 
 static int image_ihex_buffer_complete_inner(struct image *image,
-       char *lpszLine,
+       char *lpsz_line,
        struct imagesection *section)
 {
        struct image_ihex *ihex = image->type_private;
@@ -152,7 +141,7 @@ static int image_ihex_buffer_complete_inner(struct image *image,
                section[image->num_sections].size = 0x0;
                section[image->num_sections].flags = 0;
 
-               while (fileio_fgets(fileio, 1023, lpszLine) == ERROR_OK) {
+               while (fileio_fgets(fileio, 1023, lpsz_line) == ERROR_OK) {
                        uint32_t count;
                        uint32_t address;
                        uint32_t record_type;
@@ -161,10 +150,10 @@ static int image_ihex_buffer_complete_inner(struct image *image,
                        size_t bytes_read = 0;
 
                        /* skip comments and blank lines */
-                       if ((lpszLine[0] == '#') || (strlen(lpszLine + strspn(lpszLine, "\n\t\r ")) == 0))
+                       if ((lpsz_line[0] == '#') || (strlen(lpsz_line + strspn(lpsz_line, "\n\t\r ")) == 0))
                                continue;
 
-                       if (sscanf(&lpszLine[bytes_read], ":%2" SCNx32 "%4" SCNx32 "%2" SCNx32, &count,
+                       if (sscanf(&lpsz_line[bytes_read], ":%2" SCNx32 "%4" SCNx32 "%2" SCNx32, &count,
                                &address, &record_type) != 3)
                                return ERROR_IMAGE_FORMAT_ERROR;
                        bytes_read += 9;
@@ -199,7 +188,7 @@ static int image_ihex_buffer_complete_inner(struct image *image,
 
                                while (count-- > 0) {
                                        unsigned value;
-                                       sscanf(&lpszLine[bytes_read], "%2x", &value);
+                                       sscanf(&lpsz_line[bytes_read], "%2x", &value);
                                        ihex->buffer[cooked_bytes] = (uint8_t)value;
                                        cal_checksum += (uint8_t)ihex->buffer[cooked_bytes];
                                        bytes_read += 2;
@@ -225,7 +214,7 @@ static int image_ihex_buffer_complete_inner(struct image *image,
                        } else if (record_type == 2) {  /* Linear Address Record */
                                uint16_t upper_address;
 
-                               sscanf(&lpszLine[bytes_read], "%4hx", &upper_address);
+                               sscanf(&lpsz_line[bytes_read], "%4hx", &upper_address);
                                cal_checksum += (uint8_t)(upper_address >> 8);
                                cal_checksum += (uint8_t)upper_address;
                                bytes_read += 4;
@@ -257,14 +246,14 @@ static int image_ihex_buffer_complete_inner(struct image *image,
                                /* "Start Segment Address Record" will not be supported
                                 * but we must consume it, and do not create an error.  */
                                while (count-- > 0) {
-                                       sscanf(&lpszLine[bytes_read], "%2" SCNx32, &dummy);
+                                       sscanf(&lpsz_line[bytes_read], "%2" SCNx32, &dummy);
                                        cal_checksum += (uint8_t)dummy;
                                        bytes_read += 2;
                                }
                        } else if (record_type == 4) {  /* Extended Linear Address Record */
                                uint16_t upper_address;
 
-                               sscanf(&lpszLine[bytes_read], "%4hx", &upper_address);
+                               sscanf(&lpsz_line[bytes_read], "%4hx", &upper_address);
                                cal_checksum += (uint8_t)(upper_address >> 8);
                                cal_checksum += (uint8_t)upper_address;
                                bytes_read += 4;
@@ -293,7 +282,7 @@ static int image_ihex_buffer_complete_inner(struct image *image,
                        } else if (record_type == 5) {  /* Start Linear Address Record */
                                uint32_t start_address;
 
-                               sscanf(&lpszLine[bytes_read], "%8" SCNx32, &start_address);
+                               sscanf(&lpsz_line[bytes_read], "%8" SCNx32, &start_address);
                                cal_checksum += (uint8_t)(start_address >> 24);
                                cal_checksum += (uint8_t)(start_address >> 16);
                                cal_checksum += (uint8_t)(start_address >> 8);
@@ -307,7 +296,7 @@ static int image_ihex_buffer_complete_inner(struct image *image,
                                return ERROR_IMAGE_FORMAT_ERROR;
                        }
 
-                       sscanf(&lpszLine[bytes_read], "%2" SCNx32, &checksum);
+                       sscanf(&lpsz_line[bytes_read], "%2" SCNx32, &checksum);
 
                        if ((uint8_t)checksum != (uint8_t)(~cal_checksum + 1)) {
                                /* checksum failed */
@@ -317,7 +306,7 @@ static int image_ihex_buffer_complete_inner(struct image *image,
 
                        if (end_rec) {
                                end_rec = false;
-                               LOG_WARNING("continuing after end-of-file record: %.40s", lpszLine);
+                               LOG_WARNING("continuing after end-of-file record: %.40s", lpsz_line);
                        }
                }
        }
@@ -336,23 +325,23 @@ static int image_ihex_buffer_complete_inner(struct image *image,
  */
 static int image_ihex_buffer_complete(struct image *image)
 {
-       char *lpszLine = malloc(1023);
-       if (lpszLine == NULL) {
+       char *lpsz_line = malloc(1023);
+       if (!lpsz_line) {
                LOG_ERROR("Out of memory");
                return ERROR_FAIL;
        }
        struct imagesection *section = malloc(sizeof(struct imagesection) * IMAGE_MAX_SECTIONS);
-       if (section == NULL) {
-               free(lpszLine);
+       if (!section) {
+               free(lpsz_line);
                LOG_ERROR("Out of memory");
                return ERROR_FAIL;
        }
        int retval;
 
-       retval = image_ihex_buffer_complete_inner(image, lpszLine, section);
+       retval = image_ihex_buffer_complete_inner(image, lpsz_line, section);
 
        free(section);
-       free(lpszLine);
+       free(lpsz_line);
 
        return retval;
 }
@@ -374,7 +363,7 @@ static int image_elf32_read_headers(struct image *image)
 
        elf->header32 = malloc(sizeof(Elf32_Ehdr));
 
-       if (elf->header32 == NULL) {
+       if (!elf->header32) {
                LOG_ERROR("insufficient memory to perform operation");
                return ERROR_FILEIO_OPERATION_FAILED;
        }
@@ -402,7 +391,7 @@ static int image_elf32_read_headers(struct image *image)
        }
 
        elf->segments32 = malloc(elf->segment_count*sizeof(Elf32_Phdr));
-       if (elf->segments32 == NULL) {
+       if (!elf->segments32) {
                LOG_ERROR("insufficient memory to perform operation");
                return ERROR_FILEIO_OPERATION_FAILED;
        }
@@ -454,7 +443,7 @@ static int image_elf32_read_headers(struct image *image)
 
        /* alloc and fill sections array with loadable segments */
        image->sections = malloc(image->num_sections * sizeof(struct imagesection));
-       if (image->sections == NULL) {
+       if (!image->sections) {
                LOG_ERROR("insufficient memory to perform operation");
                return ERROR_FILEIO_OPERATION_FAILED;
        }
@@ -499,7 +488,7 @@ static int image_elf64_read_headers(struct image *image)
 
        elf->header64 = malloc(sizeof(Elf64_Ehdr));
 
-       if (elf->header64 == NULL) {
+       if (!elf->header64) {
                LOG_ERROR("insufficient memory to perform operation");
                return ERROR_FILEIO_OPERATION_FAILED;
        }
@@ -527,7 +516,7 @@ static int image_elf64_read_headers(struct image *image)
        }
 
        elf->segments64 = malloc(elf->segment_count*sizeof(Elf64_Phdr));
-       if (elf->segments64 == NULL) {
+       if (!elf->segments64) {
                LOG_ERROR("insufficient memory to perform operation");
                return ERROR_FILEIO_OPERATION_FAILED;
        }
@@ -579,7 +568,7 @@ static int image_elf64_read_headers(struct image *image)
 
        /* alloc and fill sections array with loadable segments */
        image->sections = malloc(image->num_sections * sizeof(struct imagesection));
-       if (image->sections == NULL) {
+       if (!image->sections) {
                LOG_ERROR("insufficient memory to perform operation");
                return ERROR_FILEIO_OPERATION_FAILED;
        }
@@ -596,7 +585,7 @@ static int image_elf64_read_headers(struct image *image)
                                image->sections[j].base_address = field64(elf,
                                                elf->segments64[i].p_paddr);
                        image->sections[j].private = &elf->segments64[i];
-                       image->sections[j].flags = field32(elf, elf->segments64[i].p_flags);
+                       image->sections[j].flags = field64(elf, elf->segments64[i].p_flags);
                        j++;
                }
        }
@@ -755,7 +744,7 @@ static int image_elf_read_section(struct image *image,
 }
 
 static int image_mot_buffer_complete_inner(struct image *image,
-       char *lpszLine,
+       char *lpsz_line,
        struct imagesection *section)
 {
        struct image_mot *mot = image->type_private;
@@ -784,7 +773,7 @@ static int image_mot_buffer_complete_inner(struct image *image,
                section[image->num_sections].size = 0x0;
                section[image->num_sections].flags = 0;
 
-               while (fileio_fgets(fileio, 1023, lpszLine) == ERROR_OK) {
+               while (fileio_fgets(fileio, 1023, lpsz_line) == ERROR_OK) {
                        uint32_t count;
                        uint32_t address;
                        uint32_t record_type;
@@ -793,11 +782,11 @@ static int image_mot_buffer_complete_inner(struct image *image,
                        uint32_t bytes_read = 0;
 
                        /* skip comments and blank lines */
-                       if ((lpszLine[0] == '#') || (strlen(lpszLine + strspn(lpszLine, "\n\t\r ")) == 0))
+                       if ((lpsz_line[0] == '#') || (strlen(lpsz_line + strspn(lpsz_line, "\n\t\r ")) == 0))
                                continue;
 
                        /* get record type and record length */
-                       if (sscanf(&lpszLine[bytes_read], "S%1" SCNx32 "%2" SCNx32, &record_type,
+                       if (sscanf(&lpsz_line[bytes_read], "S%1" SCNx32 "%2" SCNx32, &record_type,
                                &count) != 2)
                                return ERROR_IMAGE_FORMAT_ERROR;
 
@@ -809,18 +798,18 @@ static int image_mot_buffer_complete_inner(struct image *image,
 
                        if (record_type == 0) {
                                /* S0 - starting record (optional) */
-                               int iValue;
+                               int value;
 
                                while (count-- > 0) {
-                                       sscanf(&lpszLine[bytes_read], "%2x", &iValue);
-                                       cal_checksum += (uint8_t)iValue;
+                                       sscanf(&lpsz_line[bytes_read], "%2x", &value);
+                                       cal_checksum += (uint8_t)value;
                                        bytes_read += 2;
                                }
                        } else if (record_type >= 1 && record_type <= 3) {
                                switch (record_type) {
                                        case 1:
                                                /* S1 - 16 bit address data record */
-                                               sscanf(&lpszLine[bytes_read], "%4" SCNx32, &address);
+                                               sscanf(&lpsz_line[bytes_read], "%4" SCNx32, &address);
                                                cal_checksum += (uint8_t)(address >> 8);
                                                cal_checksum += (uint8_t)address;
                                                bytes_read += 4;
@@ -829,7 +818,7 @@ static int image_mot_buffer_complete_inner(struct image *image,
 
                                        case 2:
                                                /* S2 - 24 bit address data record */
-                                               sscanf(&lpszLine[bytes_read], "%6" SCNx32, &address);
+                                               sscanf(&lpsz_line[bytes_read], "%6" SCNx32, &address);
                                                cal_checksum += (uint8_t)(address >> 16);
                                                cal_checksum += (uint8_t)(address >> 8);
                                                cal_checksum += (uint8_t)address;
@@ -839,7 +828,7 @@ static int image_mot_buffer_complete_inner(struct image *image,
 
                                        case 3:
                                                /* S3 - 32 bit address data record */
-                                               sscanf(&lpszLine[bytes_read], "%8" SCNx32, &address);
+                                               sscanf(&lpsz_line[bytes_read], "%8" SCNx32, &address);
                                                cal_checksum += (uint8_t)(address >> 24);
                                                cal_checksum += (uint8_t)(address >> 16);
                                                cal_checksum += (uint8_t)(address >> 8);
@@ -868,7 +857,7 @@ static int image_mot_buffer_complete_inner(struct image *image,
 
                                while (count-- > 0) {
                                        unsigned value;
-                                       sscanf(&lpszLine[bytes_read], "%2x", &value);
+                                       sscanf(&lpsz_line[bytes_read], "%2x", &value);
                                        mot->buffer[cooked_bytes] = (uint8_t)value;
                                        cal_checksum += (uint8_t)mot->buffer[cooked_bytes];
                                        bytes_read += 2;
@@ -881,7 +870,7 @@ static int image_mot_buffer_complete_inner(struct image *image,
                                uint32_t dummy;
 
                                while (count-- > 0) {
-                                       sscanf(&lpszLine[bytes_read], "%2" SCNx32, &dummy);
+                                       sscanf(&lpsz_line[bytes_read], "%2" SCNx32, &dummy);
                                        cal_checksum += (uint8_t)dummy;
                                        bytes_read += 2;
                                }
@@ -906,7 +895,7 @@ static int image_mot_buffer_complete_inner(struct image *image,
                        }
 
                        /* account for checksum, will always be 0xFF */
-                       sscanf(&lpszLine[bytes_read], "%2" SCNx32, &checksum);
+                       sscanf(&lpsz_line[bytes_read], "%2" SCNx32, &checksum);
                        cal_checksum += (uint8_t)checksum;
 
                        if (cal_checksum != 0xFF) {
@@ -917,7 +906,7 @@ static int image_mot_buffer_complete_inner(struct image *image,
 
                        if (end_rec) {
                                end_rec = false;
-                               LOG_WARNING("continuing after end-of-file record: %.40s", lpszLine);
+                               LOG_WARNING("continuing after end-of-file record: %.40s", lpsz_line);
                        }
                }
        }
@@ -936,23 +925,23 @@ static int image_mot_buffer_complete_inner(struct image *image,
  */
 static int image_mot_buffer_complete(struct image *image)
 {
-       char *lpszLine = malloc(1023);
-       if (lpszLine == NULL) {
+       char *lpsz_line = malloc(1023);
+       if (!lpsz_line) {
                LOG_ERROR("Out of memory");
                return ERROR_FAIL;
        }
        struct imagesection *section = malloc(sizeof(struct imagesection) * IMAGE_MAX_SECTIONS);
-       if (section == NULL) {
-               free(lpszLine);
+       if (!section) {
+               free(lpsz_line);
                LOG_ERROR("Out of memory");
                return ERROR_FAIL;
        }
        int retval;
 
-       retval = image_mot_buffer_complete_inner(image, lpszLine, section);
+       retval = image_mot_buffer_complete_inner(image, lpsz_line, section);
 
        free(section);
-       free(lpszLine);
+       free(lpsz_line);
 
        return retval;
 }
@@ -1018,7 +1007,7 @@ int image_open(struct image *image, const char *url, const char *type_string)
        } else if (image->type == IMAGE_MEMORY) {
                struct target *target = get_target(url);
 
-               if (target == NULL) {
+               if (!target) {
                        LOG_ERROR("target '%s' not defined", url);
                        return ERROR_FAIL;
                }
@@ -1168,7 +1157,7 @@ int image_read_section(struct image *image,
        return ERROR_OK;
 }
 
-int image_add_section(struct image *image, target_addr_t base, uint32_t size, int flags, uint8_t const *data)
+int image_add_section(struct image *image, target_addr_t base, uint32_t size, uint64_t flags, uint8_t const *data)
 {
        struct imagesection *section;