flash/nor: improved API of flash_driver.info & fixed buffer overruns
[openocd.git] / src / flash / nor / str7x.c
index d8a4cd498b05fd73b711d1007059813bfafe6c68..958b0fa2b7d475109549d081576550b5aec7027f 100644 (file)
@@ -19,9 +19,7 @@
  *   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, write to the                         *
- *   Free Software Foundation, Inc.,                                       *
- *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.           *
+ *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
  ***************************************************************************/
 
 #ifdef HAVE_CONFIG_H
@@ -140,7 +138,7 @@ static int str7x_build_block_list(struct flash_bank *bank)
        struct str7x_flash_bank *str7x_info = bank->driver_priv;
 
        int i;
-       int num_sectors;
+       unsigned int num_sectors;
        int b0_sectors = 0, b1_sectors = 0;
 
        switch (bank->size) {
@@ -308,7 +306,6 @@ static int str7x_protect_check(struct flash_bank *bank)
        struct str7x_flash_bank *str7x_info = bank->driver_priv;
        struct target *target = bank->target;
 
-       int i;
        uint32_t flash_flags;
 
        if (bank->target->state != TARGET_HALTED) {
@@ -321,7 +318,7 @@ static int str7x_protect_check(struct flash_bank *bank)
        if (retval != ERROR_OK)
                return retval;
 
-       for (i = 0; i < bank->num_sectors; i++) {
+       for (unsigned int i = 0; i < bank->num_sectors; i++) {
                if (flash_flags & str7x_info->sector_bits[i])
                        bank->sectors[i].is_protected = 0;
                else
@@ -331,12 +328,12 @@ static int str7x_protect_check(struct flash_bank *bank)
        return ERROR_OK;
 }
 
-static int str7x_erase(struct flash_bank *bank, int first, int last)
+static int str7x_erase(struct flash_bank *bank, unsigned int first,
+               unsigned int last)
 {
        struct str7x_flash_bank *str7x_info = bank->driver_priv;
        struct target *target = bank->target;
 
-       int i;
        uint32_t cmd;
        uint32_t sectors = 0;
        int err;
@@ -346,7 +343,7 @@ static int str7x_erase(struct flash_bank *bank, int first, int last)
                return ERROR_TARGET_NOT_HALTED;
        }
 
-       for (i = first; i <= last; i++)
+       for (unsigned int i = first; i <= last; i++)
                sectors |= str7x_info->sector_bits[i];
 
        LOG_DEBUG("sectors: 0x%" PRIx32 "", sectors);
@@ -379,17 +376,17 @@ static int str7x_erase(struct flash_bank *bank, int first, int last)
        if (err != ERROR_OK)
                return err;
 
-       for (i = first; i <= last; i++)
+       for (unsigned int i = first; i <= last; i++)
                bank->sectors[i].is_erased = 1;
 
        return ERROR_OK;
 }
 
-static int str7x_protect(struct flash_bank *bank, int set, int first, int last)
+static int str7x_protect(struct flash_bank *bank, int set, unsigned int first,
+               unsigned int last)
 {
        struct str7x_flash_bank *str7x_info = bank->driver_priv;
        struct target *target = bank->target;
-       int i;
        uint32_t cmd;
        uint32_t protect_blocks;
 
@@ -401,7 +398,7 @@ static int str7x_protect(struct flash_bank *bank, int set, int first, int last)
        protect_blocks = 0xFFFFFFFF;
 
        if (set) {
-               for (i = first; i <= last; i++)
+               for (unsigned int i = first; i <= last; i++)
                        protect_blocks &= ~(str7x_info->sector_bits[i]);
        }
 
@@ -455,7 +452,7 @@ static int str7x_write_block(struct flash_bank *bank, const uint8_t *buffer,
        struct arm_algorithm arm_algo;
        int retval = ERROR_OK;
 
-       /* see contib/loaders/flash/str7x.s for src */
+       /* see contrib/loaders/flash/str7x.s for src */
 
        static const uint32_t str7x_flash_write_code[] = {
                                        /* write:                               */
@@ -570,7 +567,6 @@ static int str7x_write(struct flash_bank *bank, const uint8_t *buffer,
        uint32_t cmd;
        int retval;
        uint32_t check_address = offset;
-       int i;
 
        if (bank->target->state != TARGET_HALTED) {
                LOG_ERROR("Target not halted");
@@ -582,7 +578,7 @@ static int str7x_write(struct flash_bank *bank, const uint8_t *buffer,
                return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
        }
 
-       for (i = 0; i < bank->num_sectors; i++) {
+       for (unsigned int i = 0; i < bank->num_sectors; i++) {
                uint32_t sec_start = bank->sectors[i].offset;
                uint32_t sec_end = sec_start + bank->sectors[i].size;
 
@@ -706,13 +702,13 @@ COMMAND_HANDLER(str7x_handle_part_id_command)
 }
 #endif
 
-static int get_str7x_info(struct flash_bank *bank, char *buf, int buf_size)
+static int get_str7x_info(struct flash_bank *bank, struct command_invocation *cmd)
 {
        /* Setting the write protection on a sector is a permanent change but it
         * can be disabled temporarily. FLASH_NVWPAR reflects the permanent
         * protection state of the sectors, not the temporary.
         */
-       snprintf(buf, buf_size, "STR7x flash protection info is only valid after a power cycle, "
+       command_print_sameline(cmd, "STR7x flash protection info is only valid after a power cycle, "
                        "clearing the protection is only temporary and may not be reflected in the current "
                        "info returned.");
        return ERROR_OK;
@@ -801,7 +797,7 @@ static const struct command_registration str7x_command_handlers[] = {
        COMMAND_REGISTRATION_DONE
 };
 
-struct flash_driver str7x_flash = {
+const struct flash_driver str7x_flash = {
        .name = "str7x",
        .commands = str7x_command_handlers,
        .flash_bank_command = str7x_flash_bank_command,
@@ -814,4 +810,5 @@ struct flash_driver str7x_flash = {
        .erase_check = default_flash_blank_check,
        .protect_check = str7x_protect_check,
        .info = get_str7x_info,
+       .free_driver_priv = default_flash_free_driver_priv,
 };

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)