comparison target-utils/libload/intelflash.c @ 411:d7f409493eb6

loadagent: intel-rewrite-sector implemented
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Tue, 17 Jun 2014 04:31:16 +0000
parents 7daea2476062
children
comparison
equal deleted inserted replaced
410:81d387690063 411:d7f409493eb6
1 /* 1 /*
2 * This module implements the INFB and INFW commands for programming 2 * This module implements the INFB and INFW commands for programming
3 * Intel-style flash memories. The syntax and operation are exactly 3 * Intel-style flash memories. The syntax and operation are exactly
4 * the same as the AMD flash counterparts AMFB and AMFW. 4 * the same as the AMD flash counterparts AMFB and AMFW.
5 *
6 * The intel-rewrite-sector command (erase+program with a minimum of
7 * vulnerability for brickable-boot Compal phones) is implemented
8 * here as well.
5 */ 9 */
6 10
7 #include <sys/types.h> 11 #include <sys/types.h>
8 #include "types.h" 12 #include "types.h"
9 13
73 (u_long) flashptr); 77 (u_long) flashptr);
74 return; 78 return;
75 } 79 }
76 } 80 }
77 } 81 }
82
83 void
84 cmd_intel_rewrite_sector(argbulk)
85 char *argbulk;
86 {
87 char *argv[4];
88 u_long srcaddr, dstaddr, len;
89 const u16 *srcptr;
90 volatile u16 *flashptr;
91 u16 stat;
92
93 if (parse_args(argbulk, 3, 3, argv, 0) < 0)
94 return;
95 if (parse_hexarg(argv[0], 8, &srcaddr) < 0) {
96 invarg: printf("ERROR: invalid argument(s)\n");
97 return;
98 }
99 if (parse_hexarg(argv[1], 8, &dstaddr) < 0)
100 goto invarg;
101 if (parse_hexarg(argv[2], 8, &len) < 0)
102 goto invarg;
103 if (srcaddr & 1 || dstaddr & 1 || len & 1) {
104 printf("ERROR: all 3 arguments must be even\n");
105 return;
106 }
107 srcptr = (const u16 *) srcaddr;
108 flashptr = (volatile u16 *) dstaddr;
109 /* unlock the flash sector first */
110 *flashptr = 0x60;
111 *flashptr = 0xD0;
112 /* clear SR */
113 *flashptr = 0x50;
114 /* erase */
115 *flashptr = 0x20;
116 *flashptr = 0xD0;
117 /* wait for erase completion */
118 for (;;) {
119 stat = *flashptr;
120 if (stat & 0x80)
121 break;
122 }
123 if (stat & 0x30) {
124 printf("ERROR: erase operation failed!\n");
125 return;
126 }
127 /* now program the new content */
128 for (; len; len -= 2) {
129 *flashptr = 0x40;
130 *flashptr = *srcptr++;
131 for (;;) {
132 stat = *flashptr;
133 if (stat & 0x80)
134 break;
135 }
136 flashptr++;
137 }
138 printf("Operation complete, final SR: %02X\n", stat & 0xFF);
139 }