-
Notifications
You must be signed in to change notification settings - Fork 1
/
scsi.c
2691 lines (2269 loc) · 91.9 KB
/
scsi.c
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
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
*
* Copyright 2018 The wookey project team <wookey@ssi.gouv.fr>
* - Ryad Benadjila
* - Arnauld Michelizza
* - Mathieu Renard
* - Philippe Thierry
* - Philippe Trebuchet
*
* This package is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* the Free Software Foundation; either version 2.1 of the License, or (at
* ur option) any later version.
*
* This package is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along
* with this package; if not, write to the Free Software Foundation, Inc., 51
* Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include "autoconf.h"
#include "libc/malloc.h"
#include "libc/stdio.h"
#include "libc/nostd.h"
#include "libc/string.h"
#include "libc/stdint.h"
#include "libc/arpa/inet.h"
#include "libc/syscall.h"
#include "libc/sync.h"
#include "libusbctrl.h"
#include "api/libusbmsc.h"
#include "usb_bbb.h"
#include "usb_control_mass_storage.h"
#include "scsi_dbg.h"
#include "scsi.h"
#include "scsi_cmd.h"
#include "scsi_resp.h"
#include "scsi_log.h"
#include "scsi_automaton.h"
#include "libc/sanhandlers.h"
#ifdef __FRAMAC__
/* requested to access entropy sources to emulate backend errors */
# include "framac/entrypoint.h"
#endif
/*
* The SCSI stack context. This is a global variable, which means
* that the SCSI stack is not reentrant (not for scsi_context write access).
* As most micro-controlers are not multicore based, this should not be
* a problem.
*/
#ifndef __FRAMAC__
scsi_context_t scsi_ctx = {
.direction = SCSI_DIRECTION_IDLE,
.line_state = SCSI_TRANSMIT_LINE_READY,
.size_to_process = 0,
.addr = 0,
.error = 0,
.queue_empty = true,
.global_buf = NULL,
.global_buf_len = 0,
.block_size = 0,
.storage_size = 0,
.state = SCSI_IDLE
};
static cdb_t queued_cdb = { 0 };
#endif
/*@
@ assigns \nothing;
@ ensures \result == &scsi_ctx;
*/
scsi_context_t *scsi_get_context(void) {
return &scsi_ctx;
};
/*@
@ requires \separated(&cbw, &scsi_ctx,&GHOST_opaque_drv_privates, &bbb_ctx);
@ requires sensekey < 0xff;
@ assigns scsi_ctx.error,
GHOST_in_eps[bbb_ctx.iface.eps[1].ep_num].state, bbb_ctx.state, scsi_ctx.state;
@ ensures scsi_ctx.state == SCSI_IDLE;
*/
#ifndef __FRAMAC__
static
#endif
void scsi_error(uint16_t sensekey, uint8_t asc, uint8_t ascq)
{
log_printf("%s: %s: status=%d\n", __func__, __func__, sensekey);
log_printf("%s: state -> Error\n", __func__);
uint32_t err = 0;
/* @ assert sensekey <= 0xff; */
err = (uint32_t)
((sensekey & 0xff) << 16 |
asc << 8 |
ascq);
scsi_ctx.error = err;
/* returning status */
usb_bbb_send_csw(CSW_STATUS_FAILED, 0);
scsi_set_state(SCSI_IDLE);
}
/*********************************************************************
* Mutexes, protection against race conditions...
********************************************************************/
/*
* \brief entering a critical section
*
* During this critical section, any ISR is postponed to avoid any
* race condition on variables shared in write-access between ISR and
* main thread. See sys_lock() syscall API documentation.
*
* Critical sections must be as short as possible to avoid border
* effects such as latency increase and ISR queue overloading.
*/
/*@
@ assigns \nothing;
*/
#ifndef __FRAMAC__
static inline
#endif
mbed_error_t enter_critical_section(void)
{
uint8_t ret;
mbed_error_t errcode = MBED_ERROR_NONE;
/* this is a syscall requiring the kernel to lock ISR for a short time */
ret = sys_lock(LOCK_ENTER); /* Enter in critical section */
if (ret != SYS_E_DONE) {
log_printf("%s: Error: failed entering critical section!\n", __func__);
errcode = MBED_ERROR_BUSY;
}
return errcode;
}
/*
* \brief leaving a critical section
*
* Reallow the execution of the previously postponed task ISR.
*/
/*@
@ assigns \nothing;
*/
#ifndef __FRAMAC__
static inline
#endif
void leave_critical_section(void)
{
sys_lock(LOCK_EXIT); /* Exit from critical section, should not
fail */
return;
}
/********* About debugging and pretty printing **************/
#if SCSI_DEBUG
/* Frama-C info: not required here as out of Frama-C proof */
static void scsi_debug_dump_cmd(cdb_t * current_cdb, uint8_t scsi_cmd)
{
if (!current_cdb) {
return;
}
if (SCSI_DEBUG < 2) {
return;
}
switch (scsi_cmd) {
case SCSI_CMD_MODE_SENSE_10:
{
log_printf("mode_sense_10:\n");
log_printf("\treserved1 : %x\n",
current_cdb->payload.cdb10_mode_sense.LLBAA);
log_printf("\tLLBAA : %x\n",
current_cdb->payload.cdb10_mode_sense.LLBAA);
log_printf("\tDBD : %x\n",
current_cdb->payload.cdb10_mode_sense.DBD);
log_printf("\treserved2 : %x\n",
current_cdb->payload.cdb10_mode_sense.reserved2);
log_printf("\tPC : %x\n",
current_cdb->payload.cdb10_mode_sense.PC);
log_printf("\tpage_code : %x\n",
current_cdb->payload.cdb10_mode_sense.page_code);
log_printf("\tsub_page_code : %x\n",
current_cdb->payload.cdb10_mode_sense.sub_page_code);
log_printf("\treserved3 : %x\n",
current_cdb->payload.cdb10_mode_sense.reserved3);
log_printf("\tallocation_length : %x\n",
current_cdb->payload.cdb10_mode_sense.allocation_length);
log_printf("\tcontrol : %x\n",
current_cdb->payload.cdb10_mode_sense.control);
break;
}
case SCSI_CMD_MODE_SENSE_6:
{
log_printf("mode_sense_6:\n");
log_printf("\tLUN : %x\n",
current_cdb->payload.cdb6_mode_sense.LUN);
log_printf("\treserved4 : %x\n",
current_cdb->payload.cdb6_mode_sense.reserved4);
log_printf("\tDBD : %x\n",
current_cdb->payload.cdb6_mode_sense.DBD);
log_printf("\treserved3 : %x\n",
current_cdb->payload.cdb6_mode_sense.reserved3);
log_printf("\tPC : %x\n",
current_cdb->payload.cdb6_mode_sense.PC);
log_printf("\tpage_code : %x\n",
current_cdb->payload.cdb6_mode_sense.page_code);
log_printf("\treserved2 : %x\n",
current_cdb->payload.cdb6_mode_sense.reserved2);
log_printf("\tallocation_length : %x\n",
current_cdb->payload.cdb6_mode_sense.allocation_length);
log_printf("\tvendor_specific : %x\n",
current_cdb->payload.cdb6_mode_sense.vendor_specific);
log_printf("\treserved1 : %x\n",
current_cdb->payload.cdb6_mode_sense.reserved1);
log_printf("\tflag : %x\n",
current_cdb->payload.cdb6_mode_sense.flag);
log_printf("\tlink : %x\n",
current_cdb->payload.cdb6_mode_sense.link);
break;
}
case SCSI_CMD_INQUIRY:
{
log_printf("current_cdbuiry:\n");
log_printf("CMDDT: %x\n",
current_cdb->payload.cdb6_inquiry.CMDDT);
log_printf("EVPD: %x\n",
current_cdb->payload.cdb6_inquiry.EVPD);
log_printf("page_code: %x\n",
current_cdb->payload.cdb6_inquiry.page_code);
log_printf("allocation_len:%x\n",
ntohs(current_cdb->payload.cdb6_inquiry.
allocation_length));
log_printf("control : %x\n",
current_cdb->payload.cdb6_inquiry.control);
break;
}
default:
break;
}
}
#endif
/******** End of debugging and pretty printing **************/
/********* About various utility functions **************/
/*
* Is the current context compatible with data reception ?
*/
/*@
@ assigns \nothing;
@ ensures ((scsi_ctx.direction == SCSI_DIRECTION_RECV ||
scsi_ctx.direction == SCSI_DIRECTION_IDLE) &&
scsi_ctx.line_state == SCSI_TRANSMIT_LINE_READY) ==> \result == \true;
@ ensures !((scsi_ctx.direction == SCSI_DIRECTION_RECV ||
scsi_ctx.direction == SCSI_DIRECTION_IDLE) &&
scsi_ctx.line_state == SCSI_TRANSMIT_LINE_READY) ==> \result == \false;
*/
#ifndef __FRAMAC__
static inline
#endif
bool scsi_is_ready_for_data_receive(void)
{
return ((scsi_ctx.direction == SCSI_DIRECTION_RECV
|| scsi_ctx.direction == SCSI_DIRECTION_IDLE)
&& scsi_ctx.line_state == SCSI_TRANSMIT_LINE_READY);
}
/*
* Is the current context compatible with data transmission ?
*/
/*@
@ assigns \nothing;
@ ensures ((scsi_ctx.direction == SCSI_DIRECTION_SEND ||
scsi_ctx.direction == SCSI_DIRECTION_IDLE) &&
scsi_ctx.line_state == SCSI_TRANSMIT_LINE_READY) ==> \result == \true;
@ ensures !((scsi_ctx.direction == SCSI_DIRECTION_SEND ||
scsi_ctx.direction == SCSI_DIRECTION_IDLE) &&
scsi_ctx.line_state == SCSI_TRANSMIT_LINE_READY) ==> \result == \false;
*/
#ifndef __FRAMAC__
static inline
#endif
bool scsi_is_ready_for_data_send(void)
{
return ((scsi_ctx.direction == SCSI_DIRECTION_SEND
|| scsi_ctx.direction == SCSI_DIRECTION_IDLE)
&& scsi_ctx.line_state == SCSI_TRANSMIT_LINE_READY);
}
/*
* Request data of given size from BULK stack.
* This function is sending an asynchronous read request. The
* read terminaison is acknowledge by a trigger on scsi_data_available()
*/
/*@
@ requires \separated(buffer + (0 .. size-1),&scsi_ctx,&cbw, &bbb_ctx,&GHOST_opaque_drv_privates);
@ assigns scsi_ctx.addr, scsi_ctx.direction, scsi_ctx.line_state,GHOST_opaque_drv_privates, bbb_ctx.state;
// due to FramaC call to scsi_data_vailable()
@ assigns GHOST_in_eps[bbb_ctx.iface.eps[1].ep_num].state, scsi_ctx.size_to_process, scsi_ctx.state;
@ behavior invbuffer:
@ assumes buffer == NULL;
@ ensures scsi_ctx.addr == \old(scsi_ctx.addr);
@ ensures scsi_ctx.direction == \old(scsi_ctx.direction);
@ ensures scsi_ctx.line_state == \old(scsi_ctx.line_state);
@ ensures GHOST_opaque_drv_privates == \old(GHOST_opaque_drv_privates);
@ ensures bbb_ctx.state == \old(bbb_ctx.state);
@ ensures \result == MBED_ERROR_INVPARAM;
@ behavior sizenull:
@ assumes buffer != NULL;
@ assumes size == 0;
@ ensures scsi_ctx.addr == \old(scsi_ctx.addr);
@ ensures scsi_ctx.direction == \old(scsi_ctx.direction);
@ ensures scsi_ctx.line_state == \old(scsi_ctx.line_state);
@ ensures GHOST_opaque_drv_privates == \old(GHOST_opaque_drv_privates);
@ ensures bbb_ctx.state == \old(bbb_ctx.state);
@ ensures \result == MBED_ERROR_INVPARAM;
@ behavior ok:
@ requires \valid(buffer + (0 .. size-1));
@ requires size > 0;
@ ensures scsi_ctx.direction == SCSI_DIRECTION_RECV;
@ ensures scsi_ctx.line_state == SCSI_TRANSMIT_LINE_BUSY;
@ ensures scsi_ctx.addr == 0; // usb_bbb_recv() postconditions are set in usb_bbb_recv() function contract
@ ensures \result == MBED_ERROR_NONE;
@ complete behaviors;
@ disjoint behaviors;
*/
#ifndef __FRAMAC__
static
#endif
mbed_error_t scsi_get_data(uint8_t *buffer, uint32_t size)
{
mbed_error_t errcode = MBED_ERROR_NONE;
#if SCSI_DEBUG > 1
log_printf("%s: size: %d \n", __func__, size);
#endif
if (buffer == NULL) {
errcode = MBED_ERROR_INVPARAM;
goto err;
}
if (size == 0) {
errcode = MBED_ERROR_INVPARAM;
goto err;
}
/* here, we wait for an asyncrhonous execution of a trigger setting the OUT EP as having
* received data.
* This trigger is scsi_data_available(), which is executed when the bbb stack is triggered
* by the driver outep interrupt in DATA mode.
* Using FramaC, we can't emulate multithreaded execution, so we synchronously execute
* this trigger, instead of waiting for its asyncrhonous execution.
*/
#ifdef __FRAMAC__
if (!scsi_is_ready_for_data_receive()) {
/* emulating asynchronous trigger */
scsi_data_available(scsi_ctx.global_buf_len);
}
#else
while (!scsi_is_ready_for_data_receive()) {
request_data_membarrier();
continue;
}
#endif
set_u8_with_membarrier(&scsi_ctx.direction, SCSI_DIRECTION_RECV);
set_u8_with_membarrier(&scsi_ctx.line_state, SCSI_TRANSMIT_LINE_BUSY);
scsi_ctx.addr = 0;
request_data_membarrier();
usb_bbb_recv(buffer, size);
err:
return errcode;
}
/*
* Request to send data of given size into the BULK stack.
* This function is sending an asynchronous write request. The
* transmission terminaison is acknowledge by a trigger on scsi_data_sent()
*/
/*@
@ requires (size > 0);
@ requires \valid_read(data + (0 .. size-1));
@ requires \separated(data + (0 .. size-1),&scsi_ctx,&cbw, &bbb_ctx,&GHOST_opaque_drv_privates);
@ assigns scsi_ctx.addr, scsi_ctx.direction, scsi_ctx.line_state,GHOST_in_eps[bbb_ctx.iface.eps[1].ep_num].state, bbb_ctx.state;
@ ensures scsi_ctx.direction == SCSI_DIRECTION_SEND;
@ ensures scsi_ctx.line_state == SCSI_TRANSMIT_LINE_BUSY;
*/
#ifndef __FRAMAC__
static
#endif
void scsi_send_data(uint8_t *data, uint32_t size)
{
#if SCSI_DEBUG > 1
log_printf("%s: size: %d \n", __func__, size);
#endif
set_u8_with_membarrier(&scsi_ctx.direction, SCSI_DIRECTION_SEND);
set_u8_with_membarrier(&scsi_ctx.line_state, SCSI_TRANSMIT_LINE_BUSY);
scsi_ctx.addr = 0;
usb_bbb_send(data, size);
return;
}
/*
* Trigger on input data available
*/
/*@
@ requires \separated(&cbw, &bbb_ctx,&GHOST_opaque_drv_privates,&scsi_ctx);
@ requires \valid_read(bbb_ctx.iface.eps + (0 .. 1));
@ assigns GHOST_in_eps[bbb_ctx.iface.eps[1].ep_num].state, bbb_ctx.state, scsi_ctx.size_to_process, scsi_ctx.line_state, scsi_ctx.direction, scsi_ctx.state;
@ behavior buffer_bigger_than_sizetoprocess:
@ assumes (size < scsi_ctx.size_to_process);
@ ensures scsi_ctx.size_to_process == (\old(scsi_ctx.size_to_process) - \old(size));
@ ensures scsi_ctx.line_state == SCSI_TRANSMIT_LINE_READY;
@ ensures scsi_ctx.direction == \old(scsi_ctx.direction);
@ ensures GHOST_opaque_drv_privates == \old(GHOST_opaque_drv_privates);
@ ensures bbb_ctx.state == \old(bbb_ctx.state);
@ ensures scsi_ctx.state == \old(scsi_ctx.state);
@ behavior size_smaller_than_buffer:
@ assumes (size >= scsi_ctx.size_to_process);
@ ensures scsi_ctx.size_to_process == 0;
@ ensures scsi_ctx.line_state == SCSI_TRANSMIT_LINE_READY;
@ ensures scsi_ctx.direction == SCSI_DIRECTION_IDLE;
@ ensures scsi_ctx.state == SCSI_IDLE;
@ complete behaviors;
@ disjoint behaviors;
*/
#ifndef __FRAMAC__
static
#endif
void scsi_data_available(uint32_t size)
{
#if SCSI_DEBUG > 1
/* this function is triggered, printing trigger events is done only
* on debug level 2 */
log_printf("%s: %d\n", __func__, size);
#endif
if (size >= scsi_ctx.size_to_process) {
set_u32_with_membarrier(&scsi_ctx.size_to_process, 0);
} else {
set_u32_with_membarrier(&scsi_ctx.size_to_process, scsi_ctx.size_to_process - size);
}
set_u8_with_membarrier(&scsi_ctx.line_state, SCSI_TRANSMIT_LINE_READY);
if (scsi_ctx.size_to_process == 0) {
usb_bbb_send_csw(CSW_STATUS_SUCCESS, 0);
set_u8_with_membarrier(&scsi_ctx.direction, SCSI_DIRECTION_IDLE);
scsi_set_state(SCSI_IDLE);
}
}
/*
* Trigger on data sent by IP
*/
/*@
@ requires \separated(&cbw, &bbb_ctx,&GHOST_opaque_drv_privates,&scsi_ctx);
@ requires \valid_read(bbb_ctx.iface.eps + (0 .. 1));
@ assigns GHOST_in_eps[bbb_ctx.iface.eps[1].ep_num].state, bbb_ctx.state, scsi_ctx.size_to_process, scsi_ctx.line_state, scsi_ctx.direction, scsi_ctx.state;
@ behavior size_bigger_than_buffer:
@ assumes (scsi_ctx.size_to_process > scsi_ctx.global_buf_len);
@ ensures scsi_ctx.size_to_process == (\old(scsi_ctx.size_to_process) - \old(scsi_ctx.global_buf_len));
@ ensures scsi_ctx.line_state == SCSI_TRANSMIT_LINE_READY;
@ ensures scsi_ctx.direction == \old(scsi_ctx.direction);
@ ensures GHOST_opaque_drv_privates == \old(GHOST_opaque_drv_privates);
@ ensures bbb_ctx.state == \old(bbb_ctx.state);
@ ensures scsi_ctx.state == \old(scsi_ctx.state);
@ behavior size_smaller_than_buffer:
@ assumes (scsi_ctx.size_to_process <= scsi_ctx.global_buf_len);
@ ensures scsi_ctx.size_to_process == 0;
@ ensures scsi_ctx.line_state == SCSI_TRANSMIT_LINE_READY;
@ ensures scsi_ctx.direction == SCSI_DIRECTION_IDLE;
@ ensures scsi_ctx.state == SCSI_IDLE;
@ complete behaviors;
@ disjoint behaviors;
*/
#ifndef __FRAMAC__
static
#endif
void scsi_data_sent(void)
{
#if SCSI_DEBUG > 1
/* this function is triggered, printing trigger events is done only
* on debug level 2 */
log_printf("%s\n", __func__);
#endif
if (scsi_ctx.size_to_process > scsi_ctx.global_buf_len) {
set_u32_with_membarrier(&scsi_ctx.size_to_process, scsi_ctx.size_to_process - scsi_ctx.global_buf_len);
} else {
set_u32_with_membarrier(&scsi_ctx.size_to_process, 0);
}
set_u8_with_membarrier(&scsi_ctx.line_state, SCSI_TRANSMIT_LINE_READY);
if (scsi_ctx.size_to_process == 0) {
usb_bbb_send_csw(CSW_STATUS_SUCCESS, 0);
set_u8_with_membarrier(&scsi_ctx.direction, SCSI_DIRECTION_IDLE);
scsi_set_state(SCSI_IDLE);
}
}
/*@
@ requires \separated(&cbw, response, &bbb_ctx,&GHOST_opaque_drv_privates,&scsi_ctx);
@ requires \valid_read(response);
@ assigns *response;
*/
#ifndef __FRAMAC__
static
#endif
void scsi_forge_mode_sense_response(u_mode_parameter * response,
uint8_t mode)
{
if (mode == SCSI_CMD_MODE_SENSE_6) {
#ifndef __FRAMAC__
/* FIXME: this should be, in FramaC, also done */
memset(response, 0x0, sizeof(mode_parameter6_data_t));
#endif
/* We only send back the mode parameter header with no data */
response->mode6.header.mode_data_length = 3; /* The number of bytes that follow. */
response->mode6.header.medium_type = 0; /* The media type SBC. */
response->mode6.header.block_descriptor_length = 0; /* A block descriptor length of zero indicates that no block descriptors */
/* setting shortlba */
#if 0
/* FIXME: Caching is buggy right now... */
/* setting caching mode */
response->mode6.caching.SPF = 0;
response->mode6.caching.page_code = 0x08;
response->mode6.caching.page_length = 0x12;
response->mode6.caching.WCE = 0x1;
response->mode6.caching.RCD = 0x1;
response->mode6.caching.FSW = 0x1;
response->mode6.caching.DRA = 0x1;
response->mode6.caching.NV_DIS = 0x1;
#endif
} else if (mode == SCSI_CMD_MODE_SENSE_10) {
#ifndef __FRAMAC__
/* FIXME: this should be, in FramaC, also done */
memset(response, 0x0, sizeof(mode_parameter10_data_t));
#endif
/* We only send back the mode parameter header with no data */
response->mode10.header.mode_data_length = 3; /* The number of bytes that follow. */
response->mode10.header.medium_type = 0; /* The media type SBC. */
response->mode10.header.block_descriptor_length = 0; /* A block descriptor length of zero indicates that no block descriptors */
response->mode10.header.longLBA = 0;
/* are included in the mode parameter list. */
#if 0
/* FIXME: Caching is buggy right now... */
/* setting caching mode */
response->mode10.caching.SPF = 0;
response->mode10.caching.page_code = 0x08;
response->mode10.caching.page_length = 0x12;
response->mode10.caching.WCE = 0x1;
response->mode10.caching.RCD = 0x1;
response->mode10.caching.FSW = 0x1;
response->mode10.caching.DRA = 0x1;
response->mode10.caching.NV_DIS = 0x1;
#endif
}
}
/************** End of utility functions **********************/
/*
* SCSI automaton implementation.
*
* The SCSI automaton is based on two main entry points:
* - scsi_parse_cdb, triggered by USB ISR when a SCSI command is received
* - scsi_exec_automaton, which effectively execute the SCSI command, in
* main thread mode
*
* scsi_parse_cdb enqueue received command in the command queue. This functions
* is as small as possible to reduce the ISR execution cost.
* scsi_exec_automaton check for the command queue and dequeue commands to
* execute them.
*
* All scsi_cmd_* procedures execute a given SCSI command. These procedures
* are executed through scsi_exec_automaton.
*/
#ifndef __FRAMAC__
/* XXX: we should use memory barriers instead of volatile here */
extern volatile bool reset_requested;
#endif
/*
* Enqueue any received SCSI command
* this function is executed in a handler context when a command comes from USB.
*/
/*@
@ requires cdb_len <= sizeof(cdb_t);
@ requires \valid_read(cdb + (0 .. cdb_len-1));
@ requires \separated(((uint8_t*)&queued_cdb + (0 .. cdb_len-1)), &scsi_ctx, &cdb[0 .. cdb_len-1], &reset_requested);
@ assigns scsi_ctx.queue_empty, queued_cdb;
@ behavior reset_req:
@ assumes reset_requested == \true;
@ ensures scsi_ctx.queue_empty == \true;
@ behavior ok:
@ assumes reset_requested != \true;
@ ensures scsi_ctx.queue_empty == \false;
@ complete behaviors;
@ disjoint behaviors;
*/
#ifndef __FRAMAC__
static
#endif
void scsi_parse_cdb(uint8_t *cdb, uint8_t cdb_len)
{
if (reset_requested == true) {
/* a cdb is received while the main thread as not yet cleared the reset trigger */
/* waiting for main thread to clear reset trigger */
set_bool_with_membarrier(&scsi_ctx.queue_empty, true);
request_data_membarrier();
goto err;
}
/*@ assert cdb_len ≤ sizeof(queued_cdb); */
/* Only up to 16 bytes commands are supported: bigger commands are truncated,
* See cdb_t definition in scsi_cmd.h */
#ifdef __FRAMAC__
/* INFO: here, we substitute the memcpy call with a local copy to
* validate the assign constraint. Thus, the result (fonction postconditions)
* is exactly the same. */
uint8_t *queued_gen = (uint8_t*)&queued_cdb;
/*@ assert \valid(queued_gen+(0..sizeof(cdb_t)-1)); */
/*@ assert cdb_len <= sizeof(cdb_t); */
FC_memcpy_u8(queued_gen, cdb, cdb_len);
#else
memcpy((void *) &queued_cdb, (void *) cdb, cdb_len);
request_data_membarrier();
#endif
set_bool_with_membarrier(&scsi_ctx.queue_empty, false);
err:
return;
}
/*
* Commands implementation.
*
* All commands implementation check the SCSI state automaton to validate that
* the transition is authorized, and then execute the command.
*/
/* SCSI_CMD_INQUIRY */
/*@
@ requires \valid_read(cdb);
@ requires \separated(&cbw, &bbb_ctx,&GHOST_opaque_drv_privates, cdb, &scsi_ctx);
@ requires SCSI_IDLE <= current_state <= SCSI_ERROR;
@ assigns scsi_ctx.error, GHOST_in_eps[bbb_ctx.iface.eps[1].ep_num].state,
bbb_ctx.state, scsi_ctx.state;
@ behavior badstate:
@ assumes current_state != SCSI_IDLE;
@ ensures \result == MBED_ERROR_INVSTATE;
@ behavior ok:
@ assumes current_state == SCSI_IDLE;
@ ensures (\result == MBED_ERROR_NONE || \result == MBED_ERROR_INVPARAM);
@ disjoint behaviors;
@ complete behaviors;
*/
#ifndef __FRAMAC__
static
#endif
mbed_error_t scsi_cmd_inquiry(scsi_state_t current_state, cdb_t const * const cdb)
{
mbed_error_t errcode = MBED_ERROR_NONE;
inquiry_data_t response = { 0 };
cdb6_inquiry_t const * inq;
uint8_t next_state;
log_printf("%s:\n", __func__);
/* Sanity check and next state detection */
if (!scsi_is_valid_transition(current_state, SCSI_CMD_INQUIRY)) {
/*@ assert current_state != SCSI_IDLE; */
goto invalid_transition;
}
/* @ assert current_state == SCSI_IDLE; */
next_state = scsi_next_state(current_state, SCSI_CMD_INQUIRY);
/* @ assert next_state == SCSI_IDLE; */
scsi_set_state(next_state);
/* effective transition execution (if needed) */
inq = &(cdb->payload.cdb6_inquiry);
#if SCSI_DEBUG > 1
scsi_debug_dump_cmd(cdb, SCSI_CMD_INQUIRY);
#endif
uint16_t alen = ntohs(inq->allocation_length);
/* sanitize received cmd in conformity with SCSI standard */
if (inq->EVPD == 0 && alen < 5) {
/* invalid: additional fields parameter can't be send */
/*@ assert is_invalid_inquiry(&(cdb->payload.cdb6_inquiry)); */
goto invalid_cmd;
}
if (inq->EVPD == 1 && alen < 4) {
/* invalid: additional fields parameter can't be send */
/*@ assert is_invalid_inquiry(&(cdb->payload.cdb6_inquiry)); */
goto invalid_cmd;
}
/*@ assert !is_invalid_inquiry(&(cdb->payload.cdb6_inquiry)); */
/* Most of support bits are set to 0
* version is 0 because the device does not claim conformance to any
* standard
*/
response.periph_device_type = 0x0; /* direct access block device */
response.RMB = 1; /* Removable media */
response.data_format = 2; /* < 2 obsoletes, > 2 reserved */
response.additional_len = sizeof(response) - 5; /* (36 - 5) bytes after this one remain */
response.additional_len = sizeof(response) - 5; /* (36 - 5) bytes after this one remain */
#ifdef __FRAMAC__
FC_memset_ch(response.vendor_info, 0x20, sizeof(response.vendor_info));
FC_memcpy_ch(response.vendor_info, CONFIG_USB_DEV_MANUFACTURER,
strlen(CONFIG_USB_DEV_MANUFACTURER));
FC_memset_ch(response.product_identification, 0x20,
sizeof(response.product_identification));
FC_memcpy_ch(response.product_identification, CONFIG_USB_DEV_PRODNAME,
strlen(CONFIG_USB_DEV_PRODNAME));
FC_memcpy_ch(response.product_revision, CONFIG_USB_DEV_REVISION,
strlen(CONFIG_USB_DEV_REVISION));
#else
/* empty char must be set with spaces */
memset(response.vendor_info, 0x20, sizeof(response.vendor_info));
memcpy(response.vendor_info, CONFIG_USB_DEV_MANUFACTURER,
strlen(CONFIG_USB_DEV_MANUFACTURER));
/* empty char must be set with spaces */
memset(response.product_identification, 0x20,
sizeof(response.product_identification));
memcpy(response.product_identification, CONFIG_USB_DEV_PRODNAME,
strlen(CONFIG_USB_DEV_PRODNAME));
memcpy(response.product_revision, CONFIG_USB_DEV_REVISION,
strlen(CONFIG_USB_DEV_REVISION));
#endif
log_printf("%s: %s\n", __func__, response.product_revision);
if (inq->allocation_length > 0) {
usb_bbb_send((uint8_t *) & response,
(alen < sizeof(response)) ? alen : sizeof(response));
} else {
log_printf("allocation length is 0\n");
}
return errcode;
invalid_cmd:
log_printf("%s: malformed cmd\n", __func__);
scsi_error(SCSI_SENSE_ILLEGAL_REQUEST, ASC_NO_ADDITIONAL_SENSE,
ASCQ_NO_ADDITIONAL_SENSE);
errcode = MBED_ERROR_INVPARAM;
return errcode;
invalid_transition:
log_printf("%s: invalid_transition\n", __func__);
scsi_error(SCSI_SENSE_ILLEGAL_REQUEST, ASC_NO_ADDITIONAL_SENSE,
ASCQ_NO_ADDITIONAL_SENSE);
errcode = MBED_ERROR_INVSTATE;
return errcode;
}
/*@
@ requires \separated(&cbw, &bbb_ctx,&GHOST_opaque_drv_privates, &scsi_ctx);
@ requires SCSI_IDLE <= current_state <= SCSI_ERROR;
@ requires \valid_read(current_cdb);
@ assigns GHOST_in_eps[bbb_ctx.iface.eps[1].ep_num].state, bbb_ctx.state, scsi_ctx.state ;
@ ensures \result == MBED_ERROR_NONE;
*/
#ifndef __FRAMAC__
static
#endif
mbed_error_t scsi_cmd_prevent_allow_medium_removal(scsi_state_t current_state,
cdb_t * current_cdb __attribute__((unused)))
{
mbed_error_t errcode = MBED_ERROR_NONE;
uint8_t next_state;
log_printf("%s\n", __func__);
/* no invalid transition as this CMD is allowed in both IDLE & ERROR states */
next_state = scsi_next_state(current_state, SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL);
scsi_set_state(next_state);
log_printf("%s: Prevent allow medium removal: %x\n", __func__,
current_cdb->payload.cdb10_prevent_allow_removal.prevent);
/* TODO: Add callback ? */
usb_bbb_send_csw(CSW_STATUS_SUCCESS, 0);
return errcode;
/* effective transition execution (if needed) */
}
/*@
@ requires \separated(&cbw, current_cdb, &bbb_ctx,&GHOST_opaque_drv_privates, &scsi_ctx);
@ requires \valid_read(current_cdb);
@ requires SCSI_IDLE <= current_state <= SCSI_ERROR;
@ assigns GHOST_in_eps[bbb_ctx.iface.eps[1].ep_num].state, bbb_ctx.state, scsi_ctx.error, scsi_ctx.state;
@ behavior badstate:
@ assumes current_state != SCSI_IDLE;
@ ensures \result == MBED_ERROR_INVSTATE;
@ behavior ok:
@ assumes current_state == SCSI_IDLE;
@ ensures scsi_ctx.state == SCSI_IDLE;
@ ensures (\result == MBED_ERROR_NONE || \result == MBED_ERROR_NOBACKEND);
@ disjoint behaviors;
@ complete behaviors;
*/
#ifndef __FRAMAC__
static
#endif
mbed_error_t scsi_cmd_read_format_capacities(scsi_state_t current_state,
cdb_t * current_cdb)
{
mbed_error_t errcode = MBED_ERROR_NONE;
uint8_t next_state;
log_printf("%s\n", __func__);
/* Sanity check and next state detection */
if (!scsi_is_valid_transition(current_state, SCSI_CMD_READ_FORMAT_CAPACITIES)) {
/*@ assert current_state != SCSI_IDLE; */
goto invalid_transition;
}
/* @ assert current_state == SCSI_IDLE; */
next_state = scsi_next_state(current_state, SCSI_CMD_READ_FORMAT_CAPACITIES);
/* @ assert next_state == SCSI_IDLE; */
scsi_set_state(next_state);
cdb12_read_format_capacities_t *rfc =
&(current_cdb->payload.cdb12_read_format_capacities);
uint32_t storage_size = scsi_ctx.storage_size;
if (storage_size == 0) {
/* This should not happend in a nominal way (i.e. should have been set previously */
scsi_error(SCSI_SENSE_ILLEGAL_REQUEST, ASC_NO_ADDITIONAL_SENSE,
ASCQ_NO_ADDITIONAL_SENSE);
errcode = MBED_ERROR_NOBACKEND;
goto end;
}
capacity_list_t response = {
.list_header.reserved_1 = 0,
.list_header.reserved_2 = 0,
.list_header.capacity_list_length = 8,
.cur_max_capacity.number_of_blocks = htonl(scsi_ctx.storage_size - 1),
.cur_max_capacity.reserved = 0,
.cur_max_capacity.descriptor_code = FORMATTED_MEDIA,
.cur_max_capacity.block_length = htonl(scsi_ctx.block_size),
.num_format_descriptors = 1,
.formattable_descriptor.number_of_blocks =
htonl(scsi_ctx.storage_size - 1),
.formattable_descriptor.reserved = 0,
.formattable_descriptor.block_length = htonl(scsi_ctx.block_size)
};
/* we return only the current/max capacity descriptor, no formatable capacity descriptor, making the
* response size the following: */
uint32_t size =
sizeof(capacity_list_header_t) +
sizeof(curr_max_capacity_descriptor_t) + 1 +
sizeof(formattable_capacity_descriptor_t);
if (ntohs(rfc->allocation_length_msb) > 0) {
usb_bbb_send((uint8_t *) & response,
(ntohs(rfc->allocation_length_msb) <
size) ? rfc->allocation_length_msb : size);
} else {
log_printf("allocation length is 0\n");
usb_bbb_send((uint8_t *) & response, size);
}
end:
return errcode;
invalid_transition:
log_printf("%s: invalid_transition\n", __func__);
scsi_error(SCSI_SENSE_ILLEGAL_REQUEST, ASC_NO_ADDITIONAL_SENSE,
ASCQ_NO_ADDITIONAL_SENSE);
errcode = MBED_ERROR_INVSTATE;
return errcode;
}
/*
* SCSI_CMD_READ_6
* INFO: this command is deprecated but is implemented for retrocompatibility
* with older Operating Systems
*/
/*@
@ requires \separated(&cbw, &bbb_ctx,&GHOST_opaque_drv_privates, &scsi_ctx);
@ requires \valid_read(current_cdb);
@ requires SCSI_IDLE <= current_state <= SCSI_ERROR;
@ assigns scsi_ctx, GHOST_in_eps[bbb_ctx.iface.eps[1].ep_num].state, bbb_ctx.state ;
// this assign line is the consequence of the synchronized scsi_data_sent() trigger (instead of async one)
@ assigns GHOST_in_eps[bbb_ctx.iface.eps[1].ep_num].state, bbb_ctx.state, scsi_ctx.size_to_process, scsi_ctx.line_state, scsi_ctx.direction, scsi_ctx.state;
@ behavior badstate:
@ assumes current_state != SCSI_IDLE;
@ ensures \result == MBED_ERROR_INVSTATE;
@ behavior ok:
@ assumes current_state == SCSI_IDLE;
@ ensures scsi_ctx.state == SCSI_IDLE;
@ ensures (\result == MBED_ERROR_NONE || \result == MBED_ERROR_NOSTORAGE || \result == MBED_ERROR_INVPARAM);
@ disjoint behaviors;
@ complete behaviors;
*/
#ifndef __FRAMAC__
static
#endif
mbed_error_t scsi_cmd_read_data6(scsi_state_t current_state, cdb_t * current_cdb)
{
mbed_error_t errcode = MBED_ERROR_NONE;
uint32_t num_sectors;
uint32_t total_num_sectors;
uint32_t rw_lba;