Method for automatically locking and protecting flash bootstrap program based on flag detection

The invention discloses a method for automatically locking and protecting a flash bootstrap program based on flag detection, comprising the following steps: step 1, an area is marked out in a free address of Flash to store a write protection flag; step 2, a whole machine program writes first flag data in the address for storing the write protection flag after powered on for the first time; and step 3, the program first judges the data of the stored write protection flag at the second boot after the write protection flag is set as the first flag data successfully, and if the data is the first flag data, the data in the address is changed into second flag data, and a designated area of Flash is locked after that write protection conditions are satisfied is detected. For a flash which cannot be locked by a programmer and a microprocessor with a built-in flash, the invention provides a simple method for offering write protection to a bootstrap program and other key code snippets. A flash bootstrap program can be locked automatically through a boot detection locking flag, and flash locking is quick, safe and efficient.