aboutsummaryrefslogtreecommitdiff
path: root/libflash/test/test-blocklevel.c
blob: 7a4fe19acfc3c0e8d76d1744327cfb3b44b3c7e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
/* Copyright 2013-2018 IBM Corp. */

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>

#include <libflash/blocklevel.h>

#include "../ecc.c"
#include "../blocklevel.c"

#define __unused		__attribute__((unused))

#define ERR(fmt...) fprintf(stderr, fmt)

bool libflash_debug;

static int bl_test_bad_read(struct blocklevel_device *bl __unused, uint64_t pos __unused,
		void *buf __unused, uint64_t len __unused)
{
	return FLASH_ERR_PARM_ERROR;
}

static int bl_test_read(struct blocklevel_device *bl, uint64_t pos, void *buf, uint64_t len)
{
	if (pos + len > 0x1000)
		return FLASH_ERR_PARM_ERROR;

	memcpy(buf, bl->priv + pos, len);

	return 0;
}

static int bl_test_bad_write(struct blocklevel_device *bl __unused, uint64_t pos __unused,
		const void *buf __unused, uint64_t len __unused)
{
	return FLASH_ERR_PARM_ERROR;
}

static int bl_test_write(struct blocklevel_device *bl, uint64_t pos, const void *buf, uint64_t len)
{
	if (pos + len > 0x1000)
		return FLASH_ERR_PARM_ERROR;

	memcpy(bl->priv + pos, buf, len);

	return 0;
}

static int bl_test_erase(struct blocklevel_device *bl, uint64_t pos, uint64_t len)
{
	if (pos + len > 0x1000)
		return FLASH_ERR_PARM_ERROR;

	memset(bl->priv + pos, 0xff, len);

	return 0;
}

static void dump_buf(uint8_t *buf, int start, int end, int miss)
{
	int i;

	printf("pos: value\n");
	for (i = start; i < end; i++)
		printf("%04x: %c%s\n", i, buf[i] == 0xff ? '-' : buf[i], i == miss ? " <- First missmatch" : "");
}

/*
 * Returns zero if the buffer is ok. Otherwise returns the position of
 * the mismatch. If the mismatch is at zero -1 is returned
 */
static int check_buf(uint8_t *buf, int zero_start, int zero_end)
{
	int i;

	for (i = 0; i < 0x1000; i++) {
		if (i >= zero_start && i < zero_end && buf[i] != 0xff)
			return i == 0 ? -1 : i;
		if ((i < zero_start || i >= zero_end) && buf[i] != (i % 26) + 'a')
			return i == 0 ? -1 : i;
	}

	return 0;
}

static void reset_buf(uint8_t *buf)
{
	int i;

	for (i = 0; i < 0x1000; i++) {
		/* This gives repeating a - z which will be nice to visualise */
		buf[i] = (i % 26) + 'a';
	}
}

static void print_ptr(void *ptr, int len)
{
	int i;
	char *p = ptr;

	printf("0x");
	for (i = 0; i < len; i++) {
		putchar(*p);
		if (i && i % 8 == 0) {
			putchar('\n');
			if (len - i)
				printf("0x");
		}
	}
	putchar('\n');
}

int main(void)
{
	struct blocklevel_device bl_mem = { 0 };
	struct blocklevel_device *bl = &bl_mem;
	uint64_t with_ecc[10], without_ecc[10];
	char *buf = NULL, *data = NULL;
	int i, rc, miss;

	if (blocklevel_ecc_protect(bl, 0, 0x1000)) {
		ERR("Failed to blocklevel_ecc_protect!\n");
		return 1;
	}

	/* 0x1000 -> 0x3000 should remain unprotected */

	if (blocklevel_ecc_protect(bl, 0x3000, 0x1000)) {
		ERR("Failed to blocklevel_ecc_protect(0x3000, 0x1000)\n");
		return 1;
	}
	if (blocklevel_ecc_protect(bl, 0x2f00, 0x1100)) {
		ERR("Failed to blocklevel_ecc_protect(0x2f00, 0x1100)\n");
		return 1;
	}

	/* Zero length protection */
	if (!blocklevel_ecc_protect(bl, 0x4000, 0)) {
		ERR("Shouldn't have succeeded blocklevel_ecc_protect(0x4000, 0)\n");
		return 1;
	}

	/* Minimum creatable size */
	if (blocklevel_ecc_protect(bl, 0x4000, BYTES_PER_ECC)) {
		ERR("Failed to blocklevel_ecc_protect(0x4000, BYTES_PER_ECC)\n");
		return 1;
	}

	/* Deal with overlapping protections */
	if (blocklevel_ecc_protect(bl, 0x100, 0x1000)) {
		ERR("Failed to protect overlaping region blocklevel_ecc_protect(0x100, 0x1000)\n");
		return 1;
	}

	/* Deal with overflow */
	if (!blocklevel_ecc_protect(bl, 1, 0xFFFFFFFF)) {
		ERR("Added an 'overflow' protection blocklevel_ecc_protect(1, 0xFFFFFFFF)\n");
		return 1;
	}

	/* Protect everything */
	if (blocklevel_ecc_protect(bl, 0, 0xFFFFFFFF)) {
		ERR("Couldn't protect everything blocklevel_ecc_protect(0, 0xFFFFFFFF)\n");
		return 1;
	}

	if (ecc_protected(bl, 0, 1, NULL) != 1) {
		ERR("Invaid result for ecc_protected(0, 1)\n");
		return 1;
	}

	if (ecc_protected(bl, 0, 0x1000, NULL) != 1) {
		ERR("Invalid result for ecc_protected(0, 0x1000)\n");
		return 1;
	}

	if (ecc_protected(bl, 0x100, 0x100, NULL) != 1) {
		ERR("Invalid result for ecc_protected(0x0100, 0x100)\n");
		return 1;
	}

	/* Clear the protections */
	bl->ecc_prot.n_prot = 0;
	/* Reprotect */
	if (blocklevel_ecc_protect(bl, 0x3000, 0x1000)) {
		ERR("Failed to blocklevel_ecc_protect(0x3000, 0x1000)\n");
		return 1;
	}
	/* Deal with overlapping protections */
	if (blocklevel_ecc_protect(bl, 0x100, 0x1000)) {
		ERR("Failed to protect overlaping region blocklevel_ecc_protect(0x100, 0x1000)\n");
		return 1;
	}

	if (ecc_protected(bl, 0x1000, 0, NULL) != 1) {
		ERR("Invalid result for ecc_protected(0x1000, 0)\n");
		return 1;
	}

	if (ecc_protected(bl, 0x1000, 0x1000, NULL) != -1) {
		ERR("Invalid result for ecc_protected(0x1000, 0x1000)\n");
		return 1;
	}

	if (ecc_protected(bl, 0x1000, 0x100, NULL) != 1) {
		ERR("Invalid result for ecc_protected(0x1000, 0x100)\n");
		return 1;
	}

	if (ecc_protected(bl, 0x2000, 0, NULL) != 0) {
		ERR("Invalid result for ecc_protected(0x2000, 0)\n");
		return 1;
	}

	if (ecc_protected(bl, 0x4000, 1, NULL) != 0) {
		ERR("Invalid result for ecc_protected(0x4000, 1)\n");
		return 1;
	}

	/* Check for asking for a region with mixed protection */
	if (ecc_protected(bl, 0x100, 0x2000, NULL) != -1) {
		ERR("Invalid result for ecc_protected(0x100, 0x2000)\n");
		return 1;
	}

	/* Test the auto extending of regions */
	if (blocklevel_ecc_protect(bl, 0x5000, 0x100)) {
		ERR("Failed to blocklevel_ecc_protect(0x5000, 0x100)\n");
		return 1;
	}

	if (blocklevel_ecc_protect(bl, 0x5100, 0x100)) {
		ERR("Failed to blocklevel_ecc_protect(0x5100, 0x100)\n");
		return 1;
	}

	if (blocklevel_ecc_protect(bl, 0x5200, 0x100)) {
		ERR("Failed to blocklevel_ecc_protect(0x5200, 0x100)\n");
		return 1;
	}

	if (ecc_protected(bl, 0x5120, 0x10, NULL) != 1) {
		ERR("Invalid result for ecc_protected(0x5120, 0x10)\n");
		return 1;
	}

	if (blocklevel_ecc_protect(bl, 0x4f00, 0x100)) {
		ERR("Failed to blocklevel_ecc_protected(0x4900, 0x100)\n");
		return 1;
	}

	if (blocklevel_ecc_protect(bl, 0x4900, 0x100)) {
		ERR("Failed to blocklevel_ecc_protected(0x4900, 0x100)\n");
		return 1;
	}

	if (ecc_protected(bl, 0x4920, 0x10, NULL) != 1) {
		ERR("Invalid result for ecc_protected(0x4920, 0x10)\n");
		return 1;
	}

	if (blocklevel_ecc_protect(bl, 0x5290, 0x10)) {
		ERR("Failed to blocklevel_ecc_protect(0x5290, 0x10)\n");
		return 1;
	}

	/* Test the auto extending of regions */
	if (blocklevel_ecc_protect(bl, 0x6000, 0x100)) {
		ERR("Failed to blocklevel_ecc_protect(0x6000, 0x100)\n");
		return 1;
	}

	if (blocklevel_ecc_protect(bl, 0x6200, 0x100)) {
		ERR("Failed to blocklevel_ecc_protect(0x6200, 0x100)\n");
		return 1;
	}

	/* Test ECC reading and writing being 100% transparent to the
	 * caller */
	buf = malloc(0x1000);
	data = malloc(0x100);
	if (!buf || !data) {
		ERR("Malloc failed\n");
		rc = 1;
		goto out;
	}
	memset(bl, 0, sizeof(*bl));
	bl_mem.read = &bl_test_read;
	bl_mem.write = &bl_test_write;
	bl_mem.erase = &bl_test_erase;
	bl_mem.erase_mask = 0xff;
	bl_mem.priv = buf;
	reset_buf(buf);


	/*
	 * Test 1: One full and exact erase block, this shouldn't call
	 * read or write, ensure this fails if it does.
	 */
	bl_mem.write = &bl_test_bad_write;
	bl_mem.read = &bl_test_bad_read;
	if (blocklevel_smart_erase(bl, 0x100, 0x100)) {
		ERR("Failed to blocklevel_smart_erase(0x100, 0x100)\n");
		goto out;
	}
	miss = check_buf(buf, 0x100, 0x200);
	if (miss) {
		ERR("Buffer mismatch after blocklevel_smart_erase(0x100, 0x100) at 0x%0x\n",
				miss == -1 ? 0 : miss);
		dump_buf(buf, 0xfc, 0x105, miss == -1 ? 0 : miss);
		dump_buf(buf, 0x1fc, 0x205, miss == -1 ? 0 : miss);
		goto out;
	}
	bl_mem.read = &bl_test_read;
	bl_mem.write = &bl_test_write;

	reset_buf(buf);
	/* Test 2: Only touch one erase block */
	if (blocklevel_smart_erase(bl, 0x20, 0x40)) {
		ERR("Failed to blocklevel_smart_erase(0x20, 0x40)\n");
		goto out;
	}
	miss = check_buf(buf, 0x20, 0x60);
	if (miss) {
		ERR("Buffer mismatch after blocklevel_smart_erase(0x20, 0x40) at 0x%x\n",
				miss == -1 ? 0 : miss);
		dump_buf(buf, 0x1c, 0x65, miss == -1 ? 0 : miss);
		goto out;
	}

	reset_buf(buf);
	/* Test 3: Start aligned but finish somewhere in it */
	if (blocklevel_smart_erase(bl, 0x100, 0x50)) {
		ERR("Failed to blocklevel_smart_erase(0x100, 0x50)\n");
		goto out;
	}
	miss = check_buf(buf, 0x100, 0x150);
	if (miss) {
		ERR("Buffer mismatch after blocklevel_smart_erase(0x100, 0x50) at 0x%0x\n",
				miss == -1 ? 0 : miss);
		dump_buf(buf, 0xfc, 0x105, miss == -1 ? 0 : miss);
		dump_buf(buf, 0x14c, 0x155, miss == -1 ? 0 : miss);
		goto out;
	}

	reset_buf(buf);
	/* Test 4: Start somewhere in it, finish aligned */
	if (blocklevel_smart_erase(bl, 0x50, 0xb0)) {
		ERR("Failed to blocklevel_smart_erase(0x50, 0xb0)\n");
		goto out;
	}
	miss = check_buf(buf, 0x50, 0x100);
	if (miss) {
		ERR("Buffer mismatch after blocklevel_smart_erase(0x50, 0xb0) at 0x%x\n",
				miss == -1 ? 0 : miss);
		dump_buf(buf, 0x4c, 0x55, miss == -1 ? 0 : miss);
		dump_buf(buf, 0x100, 0x105, miss == -1 ? 0 : miss);
		goto out;
	}

	reset_buf(buf);
	/* Test 5: Cover two erase blocks exactly */
	if (blocklevel_smart_erase(bl, 0x100, 0x200)) {
		ERR("Failed to blocklevel_smart_erase(0x100, 0x200)\n");
		goto out;
	}
	miss = check_buf(buf, 0x100, 0x300);
	if (miss) {
		ERR("Buffer mismatch after blocklevel_smart_erase(0x100, 0x200) at 0x%x\n",
				miss == -1 ? 0 : miss);
		dump_buf(buf, 0xfc, 0x105, miss == -1 ? 0 : miss);
		dump_buf(buf, 0x2fc, 0x305, miss == -1 ? 0 : miss);
		goto out;
	}

	reset_buf(buf);
	/* Test 6: Erase 1.5 blocks (start aligned) */
	if (blocklevel_smart_erase(bl, 0x100, 0x180)) {
		ERR("Failed to blocklevel_smart_erase(0x100, 0x180)\n");
		goto out;
	}
	miss = check_buf(buf, 0x100, 0x280);
	if (miss) {
		ERR("Buffer mismatch after blocklevel_smart_erase(0x100, 0x180) at 0x%x\n",
				miss == -1 ? 0 : miss);
		dump_buf(buf, 0xfc, 0x105, miss == -1 ? 0 : miss);
		dump_buf(buf, 0x27c, 0x285, miss == -1 ? 0 : miss);
		goto out;
	}

	reset_buf(buf);
	/* Test 7: Erase 1.5 blocks (end aligned) */
	if (blocklevel_smart_erase(bl, 0x80, 0x180)) {
		ERR("Failed to blocklevel_smart_erase(0x80, 0x180)\n");
		goto out;
	}
	miss = check_buf(buf, 0x80, 0x200);
	if (miss) {
		ERR("Buffer mismatch after blocklevel_smart_erase(0x80, 0x180) at 0x%x\n",
				miss == -1 ? 0 : miss);
		dump_buf(buf, 0x7c, 0x85, miss == -1 ? 0 : miss);
		dump_buf(buf, 0x1fc, 0x205, miss == -1 ? 0 : miss);
		goto out;
	}

	reset_buf(buf);
	/* Test 8: Erase a big section, not aligned */
	if (blocklevel_smart_erase(bl, 0x120, 0x544)) {
		ERR("Failed to blocklevel_smart_erase(0x120, 0x544)\n");
		goto out;
	}
	miss = check_buf(buf, 0x120, 0x664);
	if (miss) {
		ERR("Buffer mismatch after blocklevel_smart_erase(0x120, 0x544) at 0x%x\n",
				miss == -1 ? 0 : miss);
		dump_buf(buf, 0x11c, 0x125, miss == -1 ? 0 : miss);
		dump_buf(buf, 0x65f, 0x669, miss == -1 ? 0 : miss);
		goto out;
	}

	bl_mem.priv = buf;
	reset_buf(buf);

	for (i = 0; i < 0x100; i++)
		data[i] = i;

	/* This really shouldn't fail */
	rc = blocklevel_ecc_protect(bl, 0, 0x100);
	if (rc) {
		ERR("Couldn't blocklevel_ecc_protect(0, 0x100)\n");
		goto out;
	}

	rc = blocklevel_write(bl, 0, data, 0x100);
	if (rc) {
		ERR("Couldn't blocklevel_write(0, 0x100)\n");
		goto out;
	}

	rc = blocklevel_write(bl, 0x200, data, 0x100);
	if (rc) {
		ERR("Couldn't blocklevel_write(0x200, 0x100)\n");
		goto out;
	}

	/*
	 * 0x50 once adjusted for the presence of ECC becomes 0x5a which
	 * is ECC aligned.
	 */
	rc = blocklevel_read(bl, 0x50, with_ecc, 8);
	if (rc) {
		ERR("Couldn't blocklevel_read(0x50, 8) with ecc rc=%d\n", rc);
		goto out;
	}
	rc = blocklevel_read(bl, 0x250, without_ecc, 8);
	if (rc) {
		ERR("Couldn't blocklevel_read(0x250, 8) without ecc rc=%d\n", rc);
		goto out;
	}
	if (memcmp(with_ecc, without_ecc, 8) || memcmp(with_ecc, &data[0x50], 8)) {
		ERR("ECC read and non-ECC read don't match or are wrong line: %d\n", __LINE__);
		print_ptr(with_ecc, 8);
		print_ptr(without_ecc, 8);
		print_ptr(&data[50], 8);
		rc = 1;
		goto out;
	}

	/*
	 * 0x50 once adjusted for the presence of ECC becomes 0x5a which
	 * is ECC aligned.
	 * So 0x4f won't be aligned!
	 */
	rc = blocklevel_read(bl, 0x4f, with_ecc, 8);
	if (rc) {
		ERR("Couldn't blocklevel_read(0x4f, 8) with ecc %d\n", rc);
		goto out;
	}
	rc = blocklevel_read(bl, 0x24f, without_ecc, 8);
	if (rc) {
		ERR("Couldn't blocklevel_read(0x24f, 8) without ecc %d\n", rc);
		goto out;
	}
	if (memcmp(with_ecc, without_ecc, 8) || memcmp(with_ecc, &data[0x4f], 8)) {
		ERR("ECC read and non-ECC read don't match or are wrong line: %d\n", __LINE__);
		print_ptr(with_ecc, 8);
		print_ptr(without_ecc, 8);
		print_ptr(&data[0x4f], 8);
		rc = 1;
		goto out;
	}

	/*
	 * 0x50 once adjusted for the presence of ECC becomes 0x5a which
	 * is ECC aligned.
	 */
	rc = blocklevel_read(bl, 0x50, with_ecc, 16);
	if (rc) {
		ERR("Couldn't blocklevel_read(0x50, 16) with ecc %d\n", rc);
		goto out;
	}
	rc = blocklevel_read(bl, 0x250, without_ecc, 16);
	if (rc) {
		ERR("Couldn't blocklevel_read(0x250, 16) without ecc %d\n", rc);
		goto out;
	}
	if (memcmp(with_ecc, without_ecc, 16)|| memcmp(with_ecc, &data[0x50], 16)) {
		ERR("(long read )ECC read and non-ECC read don't match or are wrong line: %d\n", __LINE__);
		print_ptr(with_ecc, 16);
		print_ptr(without_ecc, 16);
		print_ptr(&data[0x50], 16);
		rc = 1;
		goto out;
	}

	/*
	 * 0x50 once adjusted for the presence of ECC becomes 0x5a which
	 * is ECC aligned. So 4f won't be.
	 */
	rc = blocklevel_read(bl, 0x4f, with_ecc, 24);
	if (rc) {
		ERR("Couldn't blocklevel_read(0x4f, 24) with ecc %d\n", rc);
		goto out;
	}
	rc = blocklevel_read(bl, 0x24f, without_ecc, 24);
	if (rc) {
		ERR("Couldn't blocklevel_read(0x24f, 24) without ecc %d\n", rc);
		goto out;
	}
	if (memcmp(with_ecc, without_ecc, 24)|| memcmp(with_ecc, &data[0x4f], 24)) {
		ERR("(long read )ECC read and non-ECC read don't match or are wrong: %d\n", __LINE__);
		print_ptr(with_ecc, 24);
		print_ptr(without_ecc, 24);
		print_ptr(&data[0x4f], 24);
		rc = 1;
		goto out;
	}

	/*
	 * Now lets try to write at non ECC aligned positions
	 * Go easy first, 0x50 becomes 0x5a which is ECC byte aligned but
	 * not aligned to the start of the partition
	 */

	rc = blocklevel_write(bl, 0x50, data, 0xb0);
	if (rc) {
		ERR("Couldn't blocklevel_write()\n");
		goto out;
	}
	/* Read 8 bytes before to make sure we didn't ruin that */
	rc = blocklevel_read(bl, 0x48, with_ecc, 24);
	if (rc) {
		ERR("Couldn't blocklevel_read() with ecc %d\n", rc);
		goto out;
	}
	if (memcmp(with_ecc, data + 0x48, 8) || memcmp(with_ecc + 1, data, 16)) {
		rc = 1;
		ERR("Couldn't read back what we thought we wrote line: %d\n", __LINE__);
		print_ptr(with_ecc, 24);
		print_ptr(&data[0x48], 8);
		print_ptr(data, 16);
		goto out;
	}

	/* Ok lets get tricky */
	rc = blocklevel_write(bl, 0x31, data, 0xcf);
	if (rc) {
		ERR("Couldn't blocklevel_write(0x31, 0xcf)\n");
		goto out;
	}
	/* Read 8 bytes before to make sure we didn't ruin that */
	rc = blocklevel_read(bl, 0x29, with_ecc, 24);
	if (rc) {
		ERR("Couldn't blocklevel_read(0x29, 24) with ecc rc=%d\n", rc);
		goto out;
	}
	if (memcmp(with_ecc, &data[0x29], 8) || memcmp(with_ecc + 1, data, 16)) {
		ERR("Couldn't read back what we thought we wrote line: %d\n", __LINE__);
		print_ptr(with_ecc, 24);
		print_ptr(&data[0x29], 8);
		print_ptr(data, 16);
		rc = 1;
		goto out;
	}

	/*
	 * Rewrite the pattern that we've messed up
	 */
	rc = blocklevel_write(bl, 0, data, 0x100);
	if (rc) {
		ERR("Couldn't blocklevel_write(0, 0x100) to reset\n");
		goto out;
	}

	/* Be unalignmed as possible from now on, starting somewhat easy */
	rc = blocklevel_read(bl, 0, with_ecc, 5);
	if (rc) {
		ERR("Couldn't blocklevel_write(0, 5)\n");
		goto out;
	}
	if (memcmp(with_ecc, data, 5)) {
		ERR("blocklevel_read 5, 0) didn't match line: %d\n", __LINE__);
		print_ptr(with_ecc, 5);
		print_ptr(data, 5);
		rc = 1;
		goto out;
	}

	/* 39 is neither divisible by 8 or by 9 */
	rc = blocklevel_read(bl, 39, with_ecc, 5);
	if (rc) {
		ERR("Couldn't blocklevel_write(39, 5)\n");
		goto out;
	}
	if (memcmp(with_ecc, &data[39], 5)) {
		ERR("blocklevel_read(5, 39() didn't match line: %d\n", __LINE__);
		print_ptr(with_ecc, 5);
		print_ptr(&data[39], 5);
		rc = 1;
		goto out;
	}

	rc = blocklevel_read(bl, 0xb, &with_ecc, 39);
	if (rc) {
		ERR("Couldn't blocklevel_read(0xb, 39)\n");
		goto out;
	}
	if (memcmp(with_ecc, &data[0xb], 39)) {
		ERR("Strange sized and positioned read failed, blocklevel_read(0xb, 39) line: %d\n", __LINE__);
		print_ptr(with_ecc, 39);
		print_ptr(&data[0xb], 39);
		rc = 1;
		goto out;
	}

	rc = blocklevel_write(bl, 39, data, 50);
	if (rc) {
		ERR("Couldn't blocklevel_write(39, 50)\n");
		goto out;
	}

	rc = blocklevel_read(bl, 32, with_ecc, 39);
	if (rc) {
		ERR("Couldn't blocklevel_read(32, 39)\n");
		goto out;
	}

	if (memcmp(with_ecc, &data[32], 7) || memcmp(((char *)with_ecc) + 7, data, 32)) {
		ERR("Read back of odd placed/odd sized write failed, blocklevel_read(32, 39) line: %d\n", __LINE__);
		print_ptr(with_ecc, 39);
		print_ptr(&data[32], 7);
		print_ptr(data, 32);
		rc = 1;
		goto out;
	}

out:
	free(buf);
	free(data);
return rc;
}