LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-22-2017, 12:42 PM   #1
goodsignal
LQ Newbie
 
Registered: Jun 2017
Posts: 3

Rep: Reputation: Disabled
Arrow Overwrite 1st Byte only of every Sector


I'd like to write a script or discover a command line that will do the following:

On an SD card,
Search only the first byte of each 512 byte sector for hex 0x0A.
If 0x0A exists, overwrite with 0x00.
Stop after 10,000 sectors if none found.

I've scoured the dd command and don't see a how to use it in this way or know if it's even the right tool.

This is intended to be a super fast formatting tool for a custom data recording structure. Clearing this signature byte is all I need and in theory should be hundreds of times faster than overwriting the whole card.

Any example code or direction would be appreciated. Thanks.
 
Old 06-22-2017, 02:12 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Quote:
Originally Posted by goodsignal View Post
in theory should be hundreds of times faster than overwriting the whole card.
It won't be, in fact it will almost certainly be MUCH MUCH slower. Drives and flash chips are not accessed a single byte at a time. They are accessed in pages that are on the order of many kB.

First look at the read. Reading a full page is a single instruction, "give me page X". The card then streams it to you as fast as it can, on the order of tens of MB/s. Reading one out of every 512 bytes in a page is many instructions. "give me byte Y in page X", wait for response, "give me byte Z in page X", wait for response, etc. At that point it's not the data streaming rate that will kill you, it's the back and forth command time. I would honestly be surprised if you could send 10-100 individual "give me byte Y in page X" commands and get the resulting data any faster than you could just get the entire page in one command and one data burst.

Now look at the writes. Writing to flash is a complicated process, because you can't just overwrite a byte in flash by itself. In order to write a byte, you have to clear it first, but you can't clear a single byte. You can't even clear a single page. You have to clear an entire block (many tens of pages). That means that in order to write a single byte, you have to first read the entire block, erase the entire block, then re-write the entire block with that single byte replaced with the updated value. This is called "write amplification":
https://en.wikipedia.org/wiki/Write_amplification

You can also look at benchmarks on flash drives as a function of block size, like this:
http://www.cdrlabs.com/images/storie...pro%20atto.png

That y-axis is block size in kB. Reading/Writing blocks of 512 bytes is around 175x slower than reading/writing blocks of 64 kB.

Long story short, you're far better off just erasing the entire card than trying to work a byte at a time 10,000 times.

Last edited by suicidaleggroll; 06-22-2017 at 02:13 PM.
 
2 members found this post helpful.
Old 06-23-2017, 02:58 AM   #3
goodsignal
LQ Newbie
 
Registered: Jun 2017
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thank you for that insight. Though disappointing, I appreciate knowing better.
 
Old 06-23-2017, 05:36 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
or - you read a block, modify every 512 bytes and write back in one. That will be much faster. But still slower than a simple erase.
 
Old 06-23-2017, 08:50 AM   #5
IsaacKuo
Senior Member
 
Registered: Apr 2004
Location: Baton Rouge, Louisiana, USA
Distribution: Debian Stable
Posts: 2,546
Blog Entries: 8

Rep: Reputation: 465Reputation: 465Reputation: 465Reputation: 465Reputation: 465
Is this custom format something that you are defining yourself? In that case, you can more intelligently design the custom format to take advantage of how hard drives/SSDs work.

In particular, some sort of central block allocation table can be efficient, as opposed to sprinkling it all over the drive. With an SSD, of course, there are no seek delays. But even with a hard drive, this sort of thing tends to be more efficient because the table is cached in memory.

Where things get sticky is failure modes. If the power cuts out, what sort of state is the file system left in? Is it left in a weird corrupted state that is difficult/impossible to recover from? This stuff gets horrifically complex, which is why most people don't bother with creating a custom file recording system. Using an off-the-shelf file system greatly reduces headaches and development effort, as well as maintenance/troubleshooting effort later on.
 
Old 06-23-2017, 04:32 PM   #6
goodsignal
LQ Newbie
 
Registered: Jun 2017
Posts: 3

Original Poster
Rep: Reputation: Disabled
These SD cards are being written RAW by a 40MHz IoT device that has too little embedded memory for a legit file system. We store 512 bytes before writing a whole sector. The first byte is simply the place we chose for a signature so it can seek the next blank sector in the event of a power cycle.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] memcpy fails to copy data, but byte by byte assignment work venu_s Programming 7 07-08-2011 03:29 AM
Corrupted font after reading the 1st sector from an USB drive using dd Geriao Linux - Software 4 03-19-2010 06:38 PM
Dual Booting MBR/1st sector subaruwrx Linux - Newbie 1 11-19-2004 07:01 AM
How to grub-install to the 1st sector of /boot? subaruwrx Linux - General 2 09-23-2004 01:52 PM
MBR and/or 1st Sector of HDA1 kaputt after attemting dual boot (win/suse9.1) w/ grub Nathanael Linux - General 2 05-29-2004 07:08 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:52 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration