Curious that this question don’t actually raise any simple response on the internet because this is a common situation when you dump the full flash of a microcontroller and only want to keep the useful part of the file. To do that I use this command:
sed -i -e "$ s/\xff*$//" file.bin
The first $
make the execution of the next command only for the last line of the file to avoid removing 0xff in the middle of the file. The s/\xff*$//
is a usual substitute command that replace all the consecutive 0xff before the end of the line by nothing.