Skip to content

STM32L-Discovery (STM32L152R8T6) can't write firmware to chip with st-flash on Windows 10 #1214

@hydroconstructor

Description

@hydroconstructor
  • Programmer/board type: STM32L-Discovery, STLINK /V2-onboard
  • Operating system and version: Windows 10 x64
  • stlink tools version: 1.7.0, current develop branch
  • stlink commandline tool name: st-flash
  • Target chip and board: STM32L152R8T6, board STM32L-Discovery

Commandline output:

mmap() size_t overflow for file mb.bin

Expected/description:

Debugging revealed that problem is in file common.c, function map_file, in code below:

  if (sizeof(st.st_size) != sizeof(size_t)) {
    // on 32 bit systems, check if there is an overflow
    if (st.st_size > (off_t)SSIZE_MAX) {
      fprintf(stderr, "mmap() size_t overflow for file %s\n", path);
      goto on_error;
    }
  }

Type of st.st_size is off_t, which means long int. Value is size of firmware file and for me is 602 bytes.
SSIZE_MAX defines somewhere in libc headers. It means maximum size of signed integer. But in my system it compiled in long long int. Not 4 bytes long int as off_t, but 8 bytes long long int! So there is overflow in casting types without warnings during compile. As a result, instead of:
if (602 > 2^32-1) {
I got:
if (602 > -1) {
And then function returned with error message.

Proposed solution, which works on my computer:
In header section of common.c define macro OFF_T_MAX such a way:

#ifndef OFF_T_MAX
#define OFF_T_MAX 1073741824 // long int max value
#endif

In map_file function replace SSIZE_MAX by newly defined OFF_T_MAX.

Also please note that SSIZE_MAX defined in unistd.h in such a way:

#define ssize_t int
#ifndef SSIZE_MAX
#define SSIZE_MAX ((sizeof(ssize_t) == 4) ? 1073741824 : ‭4611686018427387904‬)
#endif

There is an error in numbers. SSIZE_MAX is max. value of signed int and must be equal 0x400... . But here it equals 0x7FFF ... in both cases, which is true for UNsigned int. For signed int both values are -1. But in my system SSIZE_MAX defined in libc, so this macro is not used.

Metadata

Metadata

Type

No type

Projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions