Skip to content

Commit a4d62ba

Browse files
Wang Haitaocomputersforpeace
authored andcommitted
mtd: map: fixed bug in 64-bit systems
Hardware: CPU: XLP832,the 64-bit OS NOR Flash:S29GL128S 128M Software: Kernel:2.6.32.41 Filesystem:JFFS2 When writing files, errors appear: Write len 182 but return retlen 180 Write of 182 bytes at 0x072c815c failed. returned -5, retlen 180 Write len 186 but return retlen 184 Write of 186 bytes at 0x072caff4 failed. returned -5, retlen 184 These errors exist only in 64-bit systems,not in 32-bit systems. After analysis, we found that the left shift operation is wrong in map_word_load_partial. For instance: unsigned char buf[3] ={0x9e,0x3a,0xea}; map_bankwidth(map) is 4; for (i=0; i < 3; i++) { int bitpos; bitpos = (map_bankwidth(map)-1-i)*8; orig.x[0] &= ~(0xff << bitpos); orig.x[0] |= buf[i] << bitpos; } The value of orig.x[0] is expected to be 0x9e3aeaff, but in this situation(64-bit System) we'll get the wrong value of 0xffffffff9e3aeaff due to the 64-bit sign extension: buf[i] is defined as "unsigned char" and the left-shift operation will convert it to the type of "signed int", so when left-shift buf[i] by 24 bits, the final result will get the wrong value: 0xffffffff9e3aeaff. If the left-shift bits are less than 24, then sign extension will not occur. Whereas the bankwidth of the nor flash we used is 4, therefore this BUG emerges. Signed-off-by: Pang Xunlei <pang.xunlei@zte.com.cn> Signed-off-by: Zhang Yi <zhang.yi20@zte.com.cn> Signed-off-by: Lu Zhongjun <lu.zhongjun@zte.com.cn> Cc: <stable@vger.kernel.org> Signed-off-by: Brian Norris <computersforpeace@gmail.com>
1 parent f83c383 commit a4d62ba

1 file changed

Lines changed: 2 additions & 2 deletions

File tree

include/linux/mtd/map.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ static inline map_word map_word_load_partial(struct map_info *map, map_word orig
365365
bitpos = (map_bankwidth(map)-1-i)*8;
366366
#endif
367367
orig.x[0] &= ~(0xff << bitpos);
368-
orig.x[0] |= buf[i-start] << bitpos;
368+
orig.x[0] |= (unsigned long)buf[i-start] << bitpos;
369369
}
370370
}
371371
return orig;
@@ -384,7 +384,7 @@ static inline map_word map_word_ff(struct map_info *map)
384384

385385
if (map_bankwidth(map) < MAP_FF_LIMIT) {
386386
int bw = 8 * map_bankwidth(map);
387-
r.x[0] = (1 << bw) - 1;
387+
r.x[0] = (1UL << bw) - 1;
388388
} else {
389389
for (i=0; i<map_words(map); i++)
390390
r.x[i] = ~0UL;

0 commit comments

Comments
 (0)